05 Inheritance MJS
05 Inheritance MJS
Inheritance
What is Inheritance?
Inheritance
The ability of a java class to ‘pass down’ all or
some of their fields, attributes, and methods to
another class that will be referred to as their child
class
Child class will be able to recognize the inherited
fields and methods, and can use it without
declaring or defining again
Inheritance
Inheritance is an implementation of
generalization-specialization class relationship or
"is-a" relationship
In short : it’s the mechanism by which one class
acquires the properties of another class
Inheritance
Denoted by arrows (hollow and triangular head)
Parent
Child Class
Class
A B C D
A B C D
A B A B C D
E F
A B C D
E F
Multiple Inheritance using
Staged Single Inheritance
Base Multiple Inheritance
Example - Method Inheritance
public class Parent{ public class Child extends Parent{
public String methodParent(){ public String methodChild(){
return "this is method Parent"; return "this is method Child";
} }
} }
System.out.println(c.methodChild());
System.out.println(p.methodChild());
} Child can access
} public method of
Parent, but not vice
versa
Example – Variable Inheritance
public class Parent{ public class Child extends Parent{
public int publicInt; public void methodChild(){
private int privateInt; publicInt = 10;
int defaultInt; privateInt = 20;
} defaultInt = 30;
}
}
System.out.println(p.toString());
System.out.println(c.toString());
}
}
Example
public class Parent{ public class Child extends Parent{
public String toString(){ public String toString(){
return "this is method Parent"; return "this method overridden
} by Child";
} }
}
System.out.println(p.toString());
System.out.println(c.toString());
}
}
Example
public class Parent{ public class Child extends Parent{
public String toString(){ public String toString(){
return "this is method Parent"; return "this method overridden
} by Child";
} }
public String toStringParent(){
return super.toString();
}
}
System.out.println(p.toString());
System.out.println(c.toString());
System.out.println(c.toStringParent());
}
}
Example
public class Parent{ public class Child extends Parent{
protected int number; private int number;
System.out.println(number);
System.out.println(super.number);
}
}
a = 15; // forbidden
b = 21; // forbidden
}
}
– represent final constants in all uppercase, using
underscore to separate words
Non-Access Modifier Final
Final modifier in Reverence Variable
– Once the variable refers to an object, the variable cannot
be re-bound to reference another object.
– But the object that it references is still mutable, if it was
originally mutable.
– private final ClassName finObject;
Non-Access Modifier Final
public class Parent{ public class Driver{
int number; public static void main(String args[]){
Parent p = new Parent();
public void setNumber(int number){ final Parent fp1 = new Parent();
this.number = number; final Parent fp2;
} fp2 = p;
Generalization
– Bottom-up view
Specialization
Bird
Specialization
Dove Gull
Bird
Eagle
Generalization
Pen
Eraser Ruler
?
Generalization
Pen
Eraser Ruler
Stationery
Specialization or Generalization ?
Eraser
Stationery
Ruler Pen
Generalisasi & Spesialisasi
When to use
Top-Down View
– to create a new classes with some behavior that already
exists in other class (or similar with other class)
– to create more specific classes or modification from a base
class
– to break down a large class to more specific classes
– to create a form of class hierarchy
Extends
– Adding new data and new methods to the existing class
Modify
– Modify the existing class by overloading its methods with
your won implementation