1)Data Hiding:
v Our internal data shouldn’t
go out directly i.e. outside person cannot access our data directly. By using
private modifier we can achieve Data Hiding.
class Account{
private double balance;
………………
}
v The main advantage of Data
Hiding is we can achieve security.
2)Abstraction:
v Hide internal
implementation just highlight the set of services is called Abstraction.
Eg:
v By using bank ATM, GUI screen bank people will highlight the set
of services what they are offering by hiding internal implementation.
v By using interfaces and abstract classes we can achieve Abstraction.
v The main advantage of abstraction are …
o
We can achieve security as
we are not highlighting out internal implementation.
o
Enhancement will become very
easy because without effecting outside world we can able to perform any type of
changes in our internal design.
o
It improves maintainability
of the application.
3)Encapsulation:
v The process of Hiding Data and corresponding methods into a single
module is called Encapsulation.
v If any class follows Data Hiding and Abstraction that class is
considered as encapsulated class.
v Encapsulation = Data Hiding + Abstraction
Eg:

v Hiding data behind methods is the central concept of
encapsulation.
v The main advantage of encapsulations are…
1) We can achieve security.
2) Enhancement will become very easy.
3) It improves maintainability of the application.
v Encapsulation increases length of the code and slows down
execution this is the main disadvantage of encapsulation.
4)Tightly encapsulated class:
v A class is said to be tightly encapsulated class if every variable
declared as private.
v Whether the class contains getter and setter methods or not and
whether these methods declared public or not these things are required to
check.
Eg:
class Account{
private double bal;
public double getBal(){
return bal;
}
}
Q) Which of the following
classes are tightly encapsulated?
public class A {
private int x =
10;
}
class B extends A {
int y =
20;
}
class C extends A {
private int z =
30;
}
A, C classes are tightly
encapsulated.
Q) Which of the following
classes are tightly encapsulated?
public class A {
int x =
10;
}
class B extends A {
private int y =
20;
}
class C extends A {
private int z =
30;
}
Note: If the parent class is not tightly encapsulated then no child
class is tightly is encapsulated.
5)IS-A Relationship:
v It is also known as inheritance.
v By using extends keyword we can implement IS-A Relationship.
v The main advantage of IS-A Relationship is reusability of the
code.
Eg:
class
Parent {
public void
m1() {
}
class
Child extends Parent {
public void
m2() {
. }
}
class
Test {
public static void
main(String[] args) {
Parent p = new
Parent();
p.m1();
p.m2();----------------------->CE:
cannot find symbol
symbol: method call
location: class parent
Child c = new Child();
c.m1();
c.m2();
Parent p1 = new
Parent();
p1.m1();
p1.m2();---------------------->CE:
cannot find symbol
symbol: method call
location: class parent
Child c1 = new
Parent();
}
}
Conclusions:
v Whatever the parent has by default available to the child hence on
the child class object we can call both parent and child class methods.
v Whatever the child has by default not available to the parent
class object we cannot call child specific methods.
v Parent class reference can be used to hold child class object but
by using that reference we can call only methods available in parent class and
child specific methods we cannot call.
v We cannot use child class reference to hold parent class object.
Approach1
without inheritance:
class VechileLoan{
100
methods;
}
class PersonalLoan{
100
methods;
}
class HousingLoan{
100
methods;
}
Approach2
with inheritance:
class Loan{
90 common methods
which are applicable for any loan type.
}
class VechileLoan extends Loan{
10
VechileLoan Specific methods;
}
class PersonalLoan extends Loan{
10 PersonalLoan
Specific methods;
}
class HousingLoan extends Loan{
10 HousingLoan
specific methods;
}
In the first approach we have to write 300 methods which increase
development time and causing code redundancy problems.
But in the 2nd
approach just we have to write only 120 methods which reduces development time
and there is no chance of code redundancy problems.
Eg:
For all java classes the most commonly required methods are
defined in Object class by making object class as the root.
SUN people made all these methods available to every java class.
For all exceptions and errors the common methods are defined in
Throwable class hence Throwable class
acts as the root for java exception hierarchy.

i.e. entire java API is developed based on inheritance concept
only
No comments:
Post a Comment