Object means real work entity such as computer, mouse, table,
book etc. Object oriented Programming is methodology or paradigram to design a
program using class and object. Object-Oriented Programming (OOP) consist some
important concepts namely Encapsulation, Polymorphism, Inheritance and Abstraction,
These feature are generally referred to as the OOP’s concept. An Object has some state and behaviour. In java state is the set of values of an
Object’s variables and behaviour of an object implement an method.
Class can be considered as the blueprint or a template for
an object. and describe properties and behaviour of that object.
Static variables and method are not purely
object oriented because they are not specific to instance (object)but common to
all instances,
Encapsulation is the process of wrapping of the data (properties) and behaviour
(methods)of an object into single unit; and single unit here is class (interface).
Note: Encapsulate in plain English meaning to enclose or be enclose in or as if in capsule. In Java Everything is enclose
within a class and interface.
Encapsulation enables data hiding, hiding irrelevant information from the users of a class
and exposing only the relevant details required by the user. We can expose
our operations hiding the details of what is needed to perform that
operation.
Example:
class Loan{
private int
duration; //private variables examples
of encapsulation
private String
loan;
private String
borrower;
private String
salary;
//public
constructor can break encapsulation instead use factory method
private Loan(int
duration, String loan, String borrower, String salary){
this.duration
= duration;
this.loan =
loan;
this.borrower
= borrower;
this.salary =
salary;
}
//no argument
consustructor omitted here
// create loan can
encapsulate loan creation logic
public Loan
createLoan(String loanType){
//processing based
on loan type and then returning loan object
return loan;
}
}
In this
same example of Encapsulation in Java you see all
member variables are made private so they are well encapsulated you can only change or access this variable directly
inside this class. if you want to allow outside world to access these variables
is better creating a getter and setter e.g. getLoan().
Base On Design Patter: Factory Pattern with is used to create a
objects and Singleton pattern is best
example for encapsulation.
Inheritance
is a process by which one object acquires the properties and behaviours of
another object. Means Inheritance describes
the parent (also called base class or super class) child (also called
sub-class or derived class) relationship between two classes.
Example: consider a Vehicle parent class and a child class Car.
Vehicle class will have properties and functionalities common for all
vehicles. Car will inherit those common properties from the Vehicle class
and then add properties which are specific to a car.
Note: Inheritance
represents the IS-A relationship
also known as parent-chield relationship.
Type of inheritance in Java:
1. Single (Class A extends Class B)
2. Multilevel (Class A Extends Class B and Class
B Extends C)
3. Hierarchical (Class A extends Class B and
Class A Extends C)
Note: Multiple
Inheritances is not supported in java through class, But we can achieve multiple
inheritances only through interface.
Polymorphism is the feature that
allows on interface to used for a general class of action.java supports different kinds of polymorphism
like overloading (compile time polymorphism) and overriding (Run time polymorphism).
When you refer to a child class object
using a Parent reference (e.g. Parent p = new Child ()) and invoke a
method, the overriden child class method will be invoked. Here, the actual
method called will depend on the object at runtime, not the reference
type.
Overriding
is not applicable for static methods or variables (static and non-static).
In case of variables (static and non-static) and static methods, when you
invoke a method using a reference type variable, the method or variable that
belong to the reference type is invoked.
x
No comments:
Post a Comment