Hiding of data, so that out side person can’t access our
data. The main advantage of data hiding is we can achieve security.
using ‘private’ modifier we can achieve data
hiding.
class datademo
{
private double amount;
………
}
Hiding implementation details is nothing but abstraction.
The main advantages of abstraction are we can achieve security as we r
not highlighting internal implementation.
using interfaces & abstract classes we
can achieve data Abstraction.
Wrapping data and methods with in classes in combination
with implementation hiding (through
access control) is often called encapsulation.
If a class follows data hiding and abstraction such type of
class is said to be ‘Encapsulated’ class.
Encapsulation = Data Hiding + Abstraction
Abstraction VS Encapsulation
·
Abstraction
is more about ‘What‘ a class can do. [Idea]
·
Encapsulation
is more about ‘How‘ to achieve that functionality. [Implementation]
class
Account {
private int balance;
public void setBalance(int balance) {
// validating the user & his permissions.
this.balance = balance;
}
public int getBalance() {
// validating the user and his permissions.
return balance;
}
}
Encapsulation essentially has both i.e. information hiding and
implementation hiding.
Tightly Encapsulated Class
A class is said to be tightly encapsulated iff all the data members declared as private.
·
The data members should private, getters, setters
& other methods are not required to be private.
·
if the parent class is not tightly encapsulated
then no child class is tightly encapsulated.
class x {
int i = 0;
}
class
y extends x {
//int i -> hides as public
private int j = 20;
}
class
z extends y {
//int i -> hides as public
private int k = 30;
}
4. Inheritance
IS-A Relationship
· Also
known as ‘Inheritance’.
·
By using extends
keyword we can implement inheritance.
·
The main advantage is reusability.
class P {
public void m1() {
System.out.println("Parent method");
}
}
class C extends P {
public void m1() {
System.out.println("Child method");
}
}
public class Test {
public static void main(String[] args) {
C
p = new C();
p.m1();
}
}
·
Every java class is a direct Child class of
‘Object’ class
Object
|
Class A
·
If our java class extends any other class, then
it is indirect child class of Object
Object
|
Class B
|
Class A extends B
·
Cycic inheritance is not allowed in java
class A extends B{
}
class B extends A{
}
Test.java:3: error: cyclic inheritance involving A
class A extends B{
^
1 error
Has-A Relationship
Has-a
relationship is one in which an object of one class is created as a
data member in another class.
class
Student
{
int sno;
String name;
Address address;
}
Uses-A Relationship
Uses-a
relationship is one in which an Object of one class is created inside a
method of another class.
class
Student {
int sno;
String name;
public static void main(String[] args) {
Address address = new Address();
}
}
Association, Aggregation and Composition?
1.Association :
We
call association those relationships whose objects have
an independent lifecycle and where there is no ownership between the objects.
Let’s take an example of a teacher and student. Multiple
students can associate with a single teacher, and a single student can
associate with multiple teachers, but both have their own lifecycles (both can
be create and delete independently); so when a teacher leaves the school, we
don’t need to delete any students, and when a student leaves the school, we
don’t need to delete any teachers.
2.Aggregation:
We
call aggregation those relationships whose objects have an independent life-cycle, but there is
ownership, and child objects cannot belong to another parent object.
Let’s take an example of a cell phone and a cell phone
battery. A single battery can belong to a phone, but if the phone stops
working, and we delete it from our database, the phone battery will not be
deleted because it may still be functional. So in aggregation, while there is
ownership, objects have their own life-cycle.
3.Composition:
We
use the term composition to refer to relationships whose
objects don’t have an independent lifecycle, and if the parent object is
deleted, all child objects will also be deleted.
Let’s take an example of the relationship between questions
and answers. Single questions can have multiple answers, and answers cannot
belong to multiple questions. If we delete questions, answers will
automatically be deleted.




0 comments:
Post a Comment