2. Without using the inheritance, Is it
possible to inherit the property of
another class?
How??
Using
Association
3. Relationship Between Classes
Inheritance
• Is-A
• extends
• Blood relation
• Tightly coupled
Association
• Is-A
• Reference variable, new
• No Blood relation
• Not Tightly coupled
4. • Association establishes a relationship between any two objects.
• Objects have an independent lifecycle i.e.) no ownership between the
objects.
Association
10. public class Association
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
String c_name = sc.next();
String e_name = sc.next();
Company c = new Company(c_name);
Employee emp = new Employee(e_name);
System.out.println(emp.getEmployeeName() + " is employee of " +
c.getCompanyName());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
11. Composition
It represents part – of relationship
Both classes are highly dependent on each other
Example:
Every human has a heart (part - of relationship)
Human and heart(Dependent on each other)
Books and Authors(Dependent on each other)
13. public class Library {
Book book;
Library(Book book){
this.book = book;
}
public static void main(String[] args) {
Book b = new Book("Shakespeare", "Julius caesar" );
Library library = new Library(b);
System.out.println(library.book.author);
System.out.println(library.book.title);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
14. // Predict the output
class Address
{
private int doorNo;
private String rem;
public int getDoorNo(){
return doorNo;
}
public void setDoorNo(int doorNo){
this.doorNo = doorNo;
}
public String getRem(){
return rem;
}
public void setRem(String rem){
this.rem = rem;
}
public String toString(){
return " Address: " + doorNo + " ," + rem;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
15. class Person
{
private String Name;
private Address address;
public Person()
{
address = new Address();
}
public void setName(String Name)
{
this.Name = Name;
}
public Address getAddress()
{
return address;
}
public String toString()
{
return "Person Name: " + Name + address ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
16. public class Main {
public static void main(String args[]) {
Person person = new Person();
person.setName("Mahendra Singh Dhoni");
person.getAddress().setDoorNo(07);
person.getAddress().setRem("XXX, Ranchi, Jharkhand.");
System.out.println(person);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Main {
public static void main(String args[]) {
Person person = new Person();
person.setName("Mahendra Singh Dhoni");
person.getAddress().setDoorNo(07);
person.getAddress().setRem("XXX, Ranchi, Jharkhand.");
System.out.println(person);
person = null;
if(person != null)
{
System.out.println(person.getAddress());
}
else
System.out.println("Address Does not Exist");
}
}
17. // Predict the output
class Engine {
public String toString() {
return "L-twin cylinder Engine";
}
}
class Bike {
String name;
private Engine engine;
public Bike(String name, Engine engine){
this.name=name;
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public String toString() {
return name + " Bike " + "uses " + engine; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
18. public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Bike bike = new Bike(“Ducati",engine);
System.out.println(engine);
System.out.println(bike);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Bike bike = new Bike(“Ducati",engine);
bike = null;
System.out.println(engine);
System.out.println(bike);
}
}
Explanation for 1st point:
The relation between object be like one to one, many-to-one, one-to-many and many-to-many.
Example:
A country has one prime minister(one to one)
Prime minister has different minister under his control (one to many)
All minister governs the people (many to many)
Explanation for 2nd point:
Both the objects can be create and delete independently.
Relation between teacher and student.
If a teacher left the school we don’t need to delete student and vice versa.
Both has independent function.
A country has one prime minister(one to one)
Prime minister has different minister under his control (one to many)
All minister serves for the welfare of the people (many to many)
There is no ownership between these object
Example :
If a prime minister dies whether the minister or people are dying?
Ans: no
If the people are dying whether the minister or prime minister are dying along with them?
Ans: no
So this example shows association between these classes
In Association, Aggregation and composition are two forms.
Aggregation is a weak bond between two classes.
Compostion is a strong bond between two classes.
Description:
Car
Input:
Face
Kirthiga Mohan
Kirthiga Mohan is employee of Face.
Description:
Here the class company and employee are two different class are associated through objects. Company can have many employees, So it is a one-to-many relationship.
Output:
Shakespeare
Julius caesar
So, If Library gets destroyed then All books within that particular library will be destroyed. i.e. book can not exist without library. That’s why it is composition.
Explanation for this Slide:
I have created a class name called Address.
In Address class I have created the object of Address i.e.) doorNo & rem(street name, city, state)
Then I am getting the value of the object using getters and setters method.
Explanation for this Slide:
Here I have a created class called Person ,
In person class I am composing the Address class by calling constructed method.
Public Person()
{
address = new Address;
}
Creating a object for address in person class this compose the property of address in person class.
Then, I am getting person name ,person address using getter method and setter method.
Output:
Person Name: Mahendra Singh Dhoni Address: 7 ,XXX, Ranchi, Jharkhand. // 1st Output.
Address does not exist //2nd Output
Explanation:
I am creating a object for Person class.
Then I am passing the value for person and address.
It will print the 1st Output.
When I delete the person class.
The address class also deleted.
This shows that one class is dependent on others.
It will print the 2nd Output.
Output:
L-twin cylinder Engine //1st Output
Ducati Bike uses L-twin cylinder Engine
L-twin cylinder Engine //2nd Output
Null
Explanation:
Engine engine = new Engine() creates a new Engine object i.e.) L-twin cylinder Engine, we are creating it outside bike.
We are creating a bike using new Bike(“Ducati”,engine); i.e. passing the bike name & outside engine object to Bike constructor.
After that we are printing engine and bike.getEngine().
Now, it will print
L-twin cylinder Engine
Ducati Bike uses L-twin cylinder Engine
Now after setting bike = null;
we are printing engine. It still gives same result i.e.) L-twin cylinder Engine
It means even after we have destroyed bike, its engine still exists.
Even, tough the engine exist but there is a strong bond between bike and engine.
We are printing bike. It print NULL.
Note:
stringto( ) method is used to return the String.