Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Without using the inheritance, Is it possible

to inherit the property of another class?

How?? Using Association


Relationship Between Classes

Inheritance Association
• Is-A • Is-A
• extends • Reference variable, new
• Blood relation • No Blood relation
• Tightly coupled • Not Tightly coupled
Association
• Association establishes a relationship between any two objects.
• Objects have an independent lifecycle i.e.) no ownership between the objects.
Association
Defense

Minister-1
Finance

External
Prime Affairs
Country Minister-2 People
Minister
Internal
Affairs

Minister-n
More…
Association
Association

Aggregation

Composition
Association

CAR

St
k ro
ea ng
W

Music_Player Engine

Aggregation Composition
Association
University
Is-A Is-A

Anna University VIT University


Has-A Has-A

Professor Branches
[Weak] (Mech, EEE, CSE)
[Strong]
1
// Predict the output
2
import java.util.*;
3
class Company
4
{
5
private String name;
6
Company(String name) {
7
this.name = name;
8
}
9
public String getCompanyName() {
10
return this.name;
11
}
12
}
13
class Employee
14
{
15
private String name;
16
Employee(String name){
17
this.name = name;
18
}
19
public String getEmployeeName() {
20
return this.name;
21
}
22
1 public class Association
2 {
3 public static void main (String[] args)
4 {
5 Scanner sc = new Scanner(System.in);
6 String c_name = sc.next();
7 String e_name = sc.next();
8 Company c = new Company(c_name);
9 Employee emp = new Employee(e_name);
10 System.out.println(emp.getEmployeeName() + " is employee of " +
11 c.getCompanyName());
12 }
13 }
14
15
16
17
18
19
20
21
22
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)


1
// Predict the output
2
3 class Book{
4
String author;
5
6 String title;
7
Book(String author, String title){
8
9 this.author = author;
10
this.title = title;
11
12 }
13
}
14
15
16
17
18
19
20
21
22
1
public class Library {
2
3 Book book;
4
Library(Book book){
5
6 this.book = book;
7
}
8
9 public static void main(String[] args) {
10
Book b = new Book("Shakespeare", "Julius caesar" );
11
12 Library library = new Library(b);
13
System.out.println(library.book.author);
14
15 System.out.println(library.book.title);
16
}
17
18 }
19
20
21
22
1 // Predict the output
2 class Address
3 {
4 private int doorNo;
5 private String rem;
6 public int getDoorNo(){
7 return doorNo;
8 }
9 public void setDoorNo(int doorNo){
10 this.doorNo = doorNo;
11 }
12 public String getRem(){
13 return rem;
14 }
15 public void setRem(String rem){
16 this.rem = rem;
17 }
18 public String toString(){
19 return " Address: " + doorNo + " ," + rem;
20 }
21 }
22
1 class Person
2 {
3 private String Name;
4 private Address address;
5 public Person()
6 {
7 address = new Address();
8 }
9 public void setName(String Name)
10 {
11 this.Name = Name;
12 }
13 public Address getAddress()
14 {
15 return address;
16 }
17 public String toString()
18 {
19 return "Person Name: " + Name + address ;
20 }
21 }
22
1 public
public class
class Main
Main {{
2 public
public static
static void
void main(String
main(String args[])
args[]) {{
3 Person
Person person
person == new
new Person();
Person();
4 person.setName("Mahendra
person.setName("Mahendra Singh
Singh Dhoni");
Dhoni");
5 person.getAddress().setDoorNo(07);
person.getAddress().setDoorNo(07);
6 person.getAddress().setRem("XXX,
person.getAddress().setRem("XXX, Ranchi,
Ranchi, Jharkhand.");
Jharkhand.");
7 System.out.println(person);
System.out.println(person);
8 } person = null;
9 } if(person != null)
10 {
11 System.out.println(person.getAddress());
12 }
13 else
14 System.out.println("Address Does not Exist");
15 }
16 }
17
18
19
20
21
22
1 // Predict the output
2 class Engine {
3 public String toString() {
4 return "L-twin cylinder Engine";
5 }
6 }
7 class Bike {
8 String name;
9 private Engine engine;
10 public Bike(String name, Engine engine){
11 this.name=name;
12 this.engine = engine;
13 }
14 public Engine getEngine() {
15 return engine;
16 }
17 public void setEngine(Engine engine) {
18 this.engine = engine;
19 }
20 public String toString() {
21 return name + " Bike " + "uses " + engine; }
22 }
1 public class Main {
2 public static void main(String[] args) {
3 Engine engine = new Engine();
4 Bike bike = new Bike(“Ducati",engine);
5 bike = null;
System.out.println(engine);
6 System.out.println(engine);
System.out.println(bike);
7 } System.out.println(bike);
8 } }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
THANK YOU

You might also like