Session 1.4 -1.6 Reference Association and Aggregation
Session 1.4 -1.6 Reference Association and Aggregation
Inheritance
Note - Base class members marked with private visibility mode are never
inherited.
Depending on the value of visibility-mode, multiple inheritance can be
performed in various ways -
• When a class inherits another class with the visibility-mode - public.
When a class is publicly inherited by another class, its public members become the
public members of the derived class, its protected members become the protected members of the
derived class, while the private members of any class cannot be inherite
• When a class inherits another class with the visibility-mode - private.
When a class is privately inherited by another class, its public members become the
private members of the derived class, its protected members become the private members of the
derived class, while the private members of any class cannot be inherited.
• When a class inherits another class with the visibility-mode - protected.
When a class is inherited by another class using the protected visibility mode, its public
members become the protected members of the derived class, its protected members become the
protected members of the derived class, while the private members of any class cannot be
inherited.The protected members cannot be directly accessed by the object of a class with a dot
operator, but can only be accessed from within the function of a class.
TYPES OF INHERITANCE IN JAVA
TYPES
//Derived Class
class B extends A
{
public void fooB()
{
//TO DO:
}
}
Example 1
Class A public static void main(String args[])
{ {
public void methodA() B obj = new B();
{
obj.methodA(); //calling super class
System.out.println("Base class method");
method
}
obj.methodB(); //calling local method
}
}
Class B extends A }
{
public void methodB()
{
System.out.println("Child class method");
}
Example 2 of Single Inheritance
Below code represents Single Inheritance in Java, where we can see the Rectangle
class is inheriting only one parent class(Shape class).
public class Shape public static void main(String args[])
{
{
Rectangle r = new Rectangle();
int length; //Assigning values to Shape class
int breadth; attributes
} r.length = 10;
r.breadth = 20;
public class Rectangle extends
//Calculate the area
Shape r.calcualteArea();
{ System.out.println("The Area of
int area; rectangle of length \""
+r.length+"\" and
public void calcualteArea() breadth \""+r.breadth+"\"
{ is \""+r.area+"\"");
area = length*breadth; } O/P:The Area of rectangle of length
} "10" and breadth "20" is "200"
}
Example 3
class Faculty
{
float salary=30000;
} O/P:
class Science extends Faculty Salary is: 30000.0
{ Bonous is: 2000.0
float bonous=2000;
public static void main(String args[])
{
Science obj=new Science();
System.out.println("Salary is:"+obj.salary);
System.out.println("Bonous is:"+obj.bonous);
}
}
MULTIPLE INHERITANCE
“Multiple Inheritance” refers to the
concept of one class extending (Or
inherits) more than one base class.
The inheritance we learnt earlier had
the concept of one base class or
parent. The problem with “multiple
inheritance” is that the derived class
will have to manage the dependency
on two base classes.
POINTS TO REMEMBER
Note 2: Most of the new OO languages like Small Talk, Java, C# do not
support Multiple inheritance. Multiple Inheritance is supported in C++.
Why Java doesn’t support Multiple
Inheritance?
// First Parent class / Error : Test is inheriting from multiple
class Parent1 // classes
{ class Test extends Parent1, Parent2
void fun()
{
{
System.out.println("Parent1");
public static void main(String args[])
} {
} Test t = new Test();
t.fun();
// Second Parent Class }
class Parent2 }
{
void fun() O/P:
{ Compile time error
System.out.println("Parent2");
}
} From the code, we see that, on calling the method fun() using
Test object will cause complications such as whether to call
Parent1’s fun() or Parent2’s fun() method.
Problems
1.The Diamond Problem:
On calling the method
fun() using Test object will cause
complications such as whether
to call Parent1’s fun() or Child’s
fun() method.
Therefore, in order to
avoid such complications Java
does not support multiple
inheritance of classes.
2. Simplicity
• Associations join one or more of one thing against one or more of another thing. A professor
might be associated with a college course (a one-to-one relationship) but also with each
student in her class (a one-to-many relationship). The students in one section might be
associated with the students in another section of the same course (a many-to-many
relationship) while all the sections of the course relate to a single course (a many-to-one
relationship).
class CarClass{ class TransportCompany{
String carName; public static void main(String args[])
int carId; {
CarClass(String name, int id) Driver obj = new
{ Driver("Andy", "Ford", 9988);
this.carName = name;
this.carId = id; System.out.println(obj.driverName+" is a
} driver of car Id: "+obj.carId);
} }
class Driver extends CarClass{ }
String driverName;
Driver(String name, String cname, int
cid){
O/P:
super(cname, cid); Andy is a driver of car Id:
this.driverName=name; 9988Andy is a driver of car Id:
} 9988
}
Points to remember
System.out.println("Child"); //p1.m2();
} //C c1=new P();reference cannot
//Child
hold Parent object
} }
}
DEMO ON INHERITANCE
class Person{ }
class Employee extends Person{ }
class Teacher extends Person{ } true
class Maths_Teacher extends Teacher{ true
public static void main(String args[]){ true
Person p=new Person(); true
Employee e=new Employee();
Teacher t=new Teacher();
Maths_Teacher m=new Maths_Teacher();
System.out.println(e instanceof Person);
System.out.println(t instanceof Person);
System.out.println(m instanceof Person);
System.out.println(m instanceof Teacher);
}
}
AGGREGATION AND COMPOSITION
Aggregation – Has - A Relationship
• Aggregation is a special form of association. It is a relationship between two classes like
association, however its a directional association
• It is strictly a one way association.
• It represents a HAS-A relationship.
• In Aggregation, both the entries can survive individually which means editing one entity
will not effect the other entity
• Composition is a restricted form of Aggregation in which two entities (or you can say classes)
are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live
and a heart needs a Human body to survive. In other words when the classes (entities) are
dependent on each other and their life span are same (if one dies then another one too) then its
a composition. Heart class has no sense if Human class is not present.
MCQs
Mcqs
1.Which is true?
A "X extends Y" is correct if and only if X is a class and Y is an
interface
B. "X extends Y" is correct if and only if X is an interface and
Y is a class
C."X extends Y" is correct if X and Y are either both classes or
both interfaces
D. "X extends Y" is correct for all combinations of X and Y
being classes and/or interfaces
class Base{ public static void main(String[] args){
public Base(){ new Derived();
System.out.print("Base"); }
} }
} 2. Find the output of the code
public class Derived extends Base{ A. HELLODerived
public Derived(){
this("HELLO"); B. HELLOBaseDerived
System.out.print("Derived");
} C. BaseHELLODerived
public Derived(String s){
System.out.print(s); D. HELLODerivedBase
}
E. Compilation Error
public class Test{
class A{
public static void main(String[]
int b=10; args){
private A(){ A a = new B();
this.b=7; System.out.println(a.f());
} }
int f(){ }
return b; 3.Find the output of the code
} A. Compilation Fails
}
B. Prints 0
class B extends A{
int b; C. Prints 10
}
D. Prints 7
E. None of these
class Parent{
4.Determine the output of
public void method(){
the code
System.out.println("Hi i am
parent"); A. Compiles successfully and
} print
}
public class Child extends Parent{ B. Compiles successfully and
protected void method(){ print
System.out.println("Hi i am
Child"); C. Compile time error
}
public static void main(String
D. Run Time error
args[]){
Child child = new Child();
child.method(); E. None of This
}
}
5.What will be the output of public class Test{
the code public static void main(String
class A{ args[]){
int i = 10;
A a = new B();
public void printValue(){
a.printValue();
System.out.print("Value-
A"); }
} }
} A. Value-B
B. Value-B Value-A
class B extends A{ C. Value-A
int i = 12; D. Value-AValue-B
public void printValue(){
E. None of these
System.out.print("Value-
B");
}
}
6.The concept of multiple inheritance is implemented in Java
by
I. Extending two or more classes.
II. Extending one class and implementing one or more
interfaces.
III. Implementing two or more interfaces.
A. Only (II)
D. Only (I)
E. Only (III)
6. void test(){
class A{
7. // insert code here
A(String s){}
8. }
9. }
A(){}
7.Which of the below code can
} be insert at line 7 to make clean
compilation ?
1. class B extends A{ A. A a = new B();
2. B(){}
3. B(String s){ B. A a = new B(5);
4. super(s);
5. } C. A a = new A(String s);
E. None of these
8. Determint the outpt
public class Test{
class Person{
public void talk(){ public static void main(String
System.out.print("I am a args[]){
Person"); Person p = new Student();
} p.talk();
} }
}
class Student extends Person{
A. I am a Person
public void talk(){
System.out.print("I am a
Student"); B. I am a Student
}
} C. I am a Person I am a Student
D. I am a Student I am a Person
public class Test{ 9. What will be the output
public static void printValue(int i, int
j, int k){ A. long
System.out.println("int"); B. int
} C. Compilation fails
D. Compilation clean but throws
public static void RuntimeException
printValue(byte...b){ E. None of these
System.out.println("long");
}
E. None of these
Answers
1. C
2. C
3. A
4. C
5. A
6. C
7. A
8. B
9. B
10.A