J8-Aggregation-Association Relationships in Java
J8-Aggregation-Association Relationships in Java
Dr B.Veera Jyothi
IT Department , CBIT.
Inheritance (IS-A relationship) in Java
another class. When a Class extends another class it inherits all non-private
language.
Inheritance (IS-A relationship) in Java
• To inherit a class, you simply incorporate the definition of one class into
another by using the extends keyword.
class Vehicle
{
......
}
}
Inheritance (IS-A relationship) in Java
• Purpose of Inheritance
• It promotes the code reusability i.e. the same methods and variables which
are defined in a parent/super/base class can be used in the
child/sub/derived class.
• It promotes polymorphism by allowing method overriding.
• Disadvantages of Inheritance
A subclass includes all of the members of its super class, it cannot access
those members of the super class that have been declared as private.
Inheritance relationship in Java language is
• Association
• Is-A
• Has-A
• None
Output of following Java Program?
Java Inheritance
class Base {
public void show() { 1.Derived:show() called
System.out.println("Base::show() called");
} 2.Base:show() called
} 3. None of the above
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
public class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Using super
super.member
Here, member can be either a method or an instance variable.