0% found this document useful (0 votes)
24 views34 pages

Association and Its Types

About Java

Uploaded by

khamzada432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views34 pages

Association and Its Types

About Java

Uploaded by

khamzada432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

LECTURE – ASSOCIATION(HAS-A)

OOP (JAVA)

MR. MUHAMMAD SALEEM


LECTURER
DEPARTMENT OF COMPUTER SCIENCE

Dawood University of Engineering and Technology, Karachi


AGENDA

 INTRODUCTION OF ASSOCATION AND ITS TYPES


 INTRODUCTION TO INHERITANCE
 TERMINOLOGY USED IN INHERITANCE
 EXTENDS KEYWORD
 TYPES OF INHERITANCE
 EXAMPLES
 IS A RELATIONSHIP
 AGGREGATION HAS A RELATIONSHIP
 SUPER KEYWORD
 FINAL KEYWORD
ASSOCIATION

 Association in Java defines the connection between two classes that are set up
through their objects.
 Association manages one-to-one, one-to-many, and many-to-many relationships.
 In Java, the multiplicity between objects is defined by the Association.
 It shows how objects communicate with each other and how they use the
functionality and services provided by that communicated object.
 Association manages one-to-one, one-to-many, many-to-one and many-to-
many relationships.
 Let's take an example of each relationship to manage by the Association.
1. A person can have only one passport. It defines the one-to-one
2. If we talk about the Association between a College and Student, a College can have many students. It
defines the one-to-many
3. A state can have several cities, and those cities are related to that single state. It defines the many-to-
one
4. A single student can associate with multiple teachers, and multiple students can also be associated
with a single teacher. Both are created or deleted independently, so it defines the many-to-many
INHERITANCE

 Inheritance in Java is a mechanism in which one object acquires all the


properties and behaviors of a parent object.
 The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes.
 When you inherit from an existing class, you can reuse methods and
fields of the parent class.
 Moreover, you can add new methods and fields in your current class
also.
TERMINOLOGY USED IN INHERITANCE

• Class:
• A class is a group of objects which have common properties.
• It is a template or blueprint from which objects are created.
• Sub Class/Child Class:
• Subclass is a class which inherits the other class.
• It is also called a derived class, extended class, or child class.
• Super Class/Parent Class:
• Superclass is the class from where a subclass inherits the features.
• It is also called a base class or a parent class.
• Reusability:
• As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when
you create a new class.
• You can use the same fields and methods already defined in the previous class.
EXTENDS KEYWORD

 The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is
to increase the functionality.
 Inheritance represents the IS-A relationship which is also
known as a parent-child relationship.
EXAMPLE

class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
TYPES OF INHERITANCE

 On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
 In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about
interfaces later.
SINGLE INHERITANCE

 When a class inherits another class, it is known as


class Animal{
a single inheritance. In the example given below, Dog
void eat(){System.out.println("eating...");}
class inherits the Animal class, so there is the single
}
inheritance.
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
MULTILEVEL INHERITANCE EXAMPLE
class Animal{
 When there is a chain of inheritance, it is known as multilevel void eat(){System.out.println("eating...");}
inheritance. As you can see in the example given below, BabyDog }
class inherits the Dog class which again inherits the Animal class, so class Dog extends Animal{
there is a multilevel inheritance. void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}

}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
HIERARCHICAL INHERITANCE EXAMPLE

 When two or more classes inherits a single class, it is


known as hierarchical inheritance. In the example given
below, Dog and Cat classes inherits the Animal class,
so there is hierarchical inheritance.
MULTIPLE INHERITANCE AND HYBRID INHERITANCE
Q) WHY MULTIPLE INHERITANCE IS NOT SUPPORTED IN JAVA?

 To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
 Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes
have the same method and you call it from child class object, there will be ambiguity to call the method of A or B
class.
 Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes.
So whether you have same method or different, there will be compile time error.
MULTIPLE INHERITANCE
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were

public static void main(String args[]){


C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
AGGREGATION IN JAVA

 If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
 Consider a situation, Employee object contains many information's such as id, name, email Id etc. It contains one
more object named address, which contains its own Information's such as city, state, country, zipcode etc. as
given below.
WHEN USE AGGREGATION?

• Code reuse is also best achieved by aggregation when there is no is-a relationship.
• Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects
involved; otherwise, aggregation is the best choice.
class Operation{ AGGREGATION EXAMPLE
int square(int n){
return n*n;
} }
class Circle{
Operation op;//aggregation
double pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square(radius);//code reus
ability (i.e. delegates the method call).
return pi*rsquare;
}

public static void main(String args[]){


Circle c=new Circle();
double result=c.area(5);
System.out.println(result);
}
}
COMPOSITION

 The Composition is a way to design or implement the "has-a" relationship.


 Composition and Inheritance both are design techniques.
 The Inheritance is used to implement the "is-a" relationship.
 The "has-a" relationship is used to ensure the code reusability in our program.
 In Composition, we use an instance variable that refers to another object.
HOW COMPOSITION WORKS

 The composition relationship of two objects is possible when one object


contains another object, and that object is fully dependent on it.
 The contained object should not exist without the existence of its parent
object.
 In a simple way, we can say it is a technique through which we can
describe the reference between two or more classes.
 And for that, we use the instance variable, which should be created
before it is used.
DIFFERENCE BETWEEN AGGREGATION AND ASSOCIATION

 Association and Aggregation are two important concepts in Object-Oriented Programming (OOP) and often used
interchangeably. However, there is a difference between the two.
 Association refers to a relationship between two objects where one object uses the services of another object. It
represents a has-a relationship, such as a teacher and a student, or a car and an engine. Association can be one-
to-one, one-to-many, many-to-one, or many-to-many.
 Aggregation refers to a special type of association where an object is a part of another object. In aggregation, an
object can exist independently of the other object. For example, a Department object and a Teacher object,
where a teacher is a part of a department, but can also exist independently.
 In summary, Association refers to the relationship between objects and Aggregation refers to a special type of
Association which represents a part-of relationship.
SUPER KEYWORD

 The super keyword in Java is a reference variable which is used to refer


immediate parent class object.
 Whenever you create the instance of subclass, an instance of parent
class is created implicitly which is referred by super reference variable.
SUPER IS USED TO REFER IMMEDIATE PARENT CLASS INSTANCE
VARIABLE.
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
2) SUPER CAN BE USED TO INVOKE PARENT CLASS METHOD
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
3) SUPER IS USED TO INVOKE PARENT CLASS CONSTRUCTOR.

class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
• As we know well that default constructor is provided by compiler automatically if there is no
constructor. But, it also adds super() as the first statement.

• Note: super() is added in each class constructor automatically by compiler if there is no


super() or this().
FINAL KEYWORD

 The final keyword in java is used to restrict the


user. The java final keyword can be used in many
context. Final can be:
1. variable
2. method
3. class
FINAL VARIABLE IN JAVA

 If you make any variable as final, you cannot class Bike9{


change the value of final variable(It will be final int speedlimit=90;//final variable
constant). void run(){
speedlimit=400;
 There is a final variable speedlimit, we are going }
to change the value of this variable, but It can't be public static void main(String args[]){
changed because final variable once assigned a Bike9 obj=new Bike9();
value can never be changed. obj.run();
}
}
2) JAVA FINAL METHOD

 If you make any method as final, you cannot override class Bike{
it. final void run(){System.out.println("running");}
}

class Honda extends Bike{


void run(){System.out.println("running safely with 100kmph")
;}

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
}

Output:Compile Time Error


JAVA FINAL CLASS

final class Bike{}


 If you make any class as final, you cannot extend it.
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");
}

public static void main(String args[]){


Honda1 honda= new Honda1();
honda.run();
}
}

Output:Compile Time Error


SOME IMPORTANT QUESTIONS

 Q) Is final method inherited?


 Can we declare a constructor final?
THANK YOU

You might also like