Saltar al contenido principal
*
Object Oriented Programming is a paradigm that provides many
concepts such as inheritance, data binding, polymorphism etc.
*
It simplifies the software development and maintenance by providing
some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Any entity that has state and behavior is known as an object.
For example: chair, pen, table, keyboard, bike etc. It can be
physical and logical.
An object has three characteristics:
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object
such as deposit, withdraw etc.
identity: Object identity is typically implemented via a
unique ID. The value of the ID is not visible to the external
user. But, it is used internally by the JVM to identify each
object uniquely.
*
Collection of objects is called class. It is a logical entity.
A class is a group of objects that has common properties. It is a
template or blueprint from which objects are created. A class in
java can contain:
data member
method
constructor
block
class and interface
*
When one object acquires all the properties and
behaviors of parent object i.e. known as
inheritance. It provides code reusability. It is used
to achieve runtime polymorphism.
*
When one task is performed by different ways i.e.
known as polymorphism. For example: to convense the
customer differently, to draw something e.g. shape or
rectangle etc.
In java, we use method overloading and method
overriding to achieve polymorphism.
Another example can be to speak something e.g. cat
speaks meaw, dog barks woof etc.
*
Hiding internal details and showing functionality
is known as abstraction. For example: phone call,
we don't know the internal processing.
In java, we use abstract class and interface to
achieve abstraction.
*
Binding (or wrapping) code and data together into a
single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is
the fully encapsulated class because all the data members
are private here.
*
By using standard Java naming conventions, you make your
code easier to read for yourself and for other
programmers. Readability of Java program is very
important. It indicates that less time is spent to figure out
what the code does.
*
Java follows camelcase syntax for naming the class,
interface, method and variable.
If name is combined with two words, second word will start
with uppercase letter always e.g. actionPerformed(),
firstName, ActionEvent, ActionListener etc.
*
*
If a class have multiple methods by same name but
different parameters, it is known as Method Overloading.
There are two ways to overload the method in java:
By changing number of arguments
By changing the data type
*
*
Constructor in java is a special type of method that is
used to initialize the object.
Rules for creating java constructor
There are basically two rules defined for the constructor.
Constructor name must be same as its class name
Constructor must have no explicit return type
*
Default constructor provides the default values to the
object like 0, null etc. depending on the type.
Java parameterized constructor
A constructor that have parameters is known as parameterized
constructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the
distinct objects.
*
There can be a lot of usage of java this keyword. In java,
this is a reference variable that refers to the current
object.
*
If subclass (child class) has the same method as declared in
the parent class, it is known as method o
Usage of Java Method Overriding
Method overriding is used to provide specific
implementation of a method that is already provided by its
super class.
Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
method must have same name as in the parent class
method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).