0% found this document useful (0 votes)
10 views

05 Inheritance MJS

Inheritance allows a child class to inherit fields and methods from a parent class. A child class can access and use the inherited fields and methods without declaring them again. Inheritance implements a generalization-specialization or "is-a" relationship between classes. In Java, single inheritance is supported using the "extends" keyword. A child class can override methods of the parent class to provide more specific implementation. The "super" keyword allows accessing the parent implementation from within the child class.

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

05 Inheritance MJS

Inheritance allows a child class to inherit fields and methods from a parent class. A child class can access and use the inherited fields and methods without declaring them again. Inheritance implements a generalization-specialization or "is-a" relationship between classes. In Java, single inheritance is supported using the "extends" keyword. A child class can override methods of the parent class to provide more specific implementation. The "super" keyword allows accessing the parent implementation from within the child class.

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

CII3B4

Pemrograman Berorientasi Objek

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

The classes that passed down the attributes and


methods are called
– parent class, super class, or base class

The classes that inherit the attributes and


methods are called
– Child class, subclass, or derived class
Types of Inheritance
Single Inheritance parent class child class

– Child class inherit from


1 parent class

parent class-1 parent class-n


Multiple Inheritance
...
– Child class inherit from
many parent classes
child class
Java Class Inheritance
Single inheritance only
Using keyword extends
class ChildClass extends ParentClass{
// child class attributes
// child class methods
}

All non-private attributes and methods are passed


on
To access the properties of parent
– Use "super" keyword
Multiple Inheritance in Java
Multiple Inheritance is handled using staged single
inheritance

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";
} }
} }

public class Driver{


public static void main(String args[]){ > This is method Parent
Parent p = new Parent(); > This is method Parent
Child c = new Child();
> This is method Child
System.out.println(p.methodParent()); > error: cannot find symbol
System.out.println(c.methodParent());

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;
}
}

error: privateInt has


private access
Example
public class Parent{ public class Child extends Parent{
public int publicInt; public void methodChild(){
private int privateInt; publicInt = 10;
int defaultInt; setPrivateInt(20);
defaultInt = 30;
public void setPrivateInt(int privateInt){ }
this.privateInt = privateInt; }
}
}
Example
package test; import test.Parent;
public class Parent{ public class Child extends Parent{
public int publicInt; public void methodChild(){
private int privateInt; publicInt = 10;
int defaultInt; setPrivateInt(20);
defaultInt = 30;
public void setPrivateInt(int privateInt){ }
this.privateInt = privateInt; }
}
}

error: defaultInt is not


public in Parent; cannot
be accessed from
outside package
Access Modifier Protected
All class in the same package
All subclass (child class) even in different package
Example
package test; import test.Parent;
public class Parent{ public class Child extends Parent{
public int publicInt; public void methodChild(){
private int privateInt; publicInt = 10;
int defaultInt; setPrivateInt(20);
protected int protectedInt; protectedInt = 30;
}
public void setPrivateInt(int privateInt){ }
this.privateInt = privateInt;
}
}

public class Driver{


public static void main(String args[]){ Modification to
Child c = new Child(); protectedInt done by
c.methodChild(); Child class
}
}
Example
package test; import test.Parent;
public class Parent{ public class Child extends Parent{
public int publicInt;
private int privateInt; }
int defaultInt;
protected int protectedInt;

public void setPrivateInt(int privateInt){


this.privateInt = privateInt;
}
}

public class Driver{


error: protectedInt has
public static void main(String args[]){
Child c = new Child(); protected access in parent
c.publicInt = 10;
c.setPrivateInt(20); (protectedInt is owned by
c.protectedInt = 40; Parent in different
} package)
}
Overriding
Redefining field or method inherited by Parent class in
Child class (Polymorphism)

subclass can implement a parent class method more


specifically based on its requirement.
When overriding a field or method, the Child class will
have access to both the original parent field/method
and the new redefined child field/method

Call parent’s field/method using keyword "super"


– Keyword "super" only available inside the child class
Example
public class Parent{ public class Child extends Parent{
public String toString(){
return "this is method Parent";
} }
}

public class Driver{


public static void main(String args[]){ > this is method Parent
Child c = new Child(); > this is method Parent
Parent p = new Parent();

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 class Driver{


public static void main(String args[]){ > this is method Parent
Child c = new Child(); > this method overridden by Child
Parent p = new Parent();

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();
}
}

public class Driver{


public static void main(String args[]){ > this is method Parent
Child c = new Child(); > this method overridden by Child
Parent p = new Parent(); > this is method Parent

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;

} public void methodChild(){


number = 10;
super.number = 20;

System.out.println(number);
System.out.println(super.number);
}
}

public class Driver{


public static void main(String args[]){ > 10
Child c = new Child(); > 20
c.methodChild();
}
}
Example
public class Parent{ public class Child extends Parent{
protected int number; private int number;

public void setNumber(int number){ public void methodChild(){


this.number = number; number = 10;
} super.number = 20;
}
public int getNumber(){ }
return number;
}
}

public class Driver{


public static void main(String args[]){ > 20
Child c = new Child();
c.methodChild();

System.out.println(c.getNumber()); setNumber() and


} getNumber() is owned by
} parent, accessing number
value from parent (super)
Example
public class Parent{ public class Child extends Parent{
protected int number; private int number;

public void setNumber(int number){ public void methodChild(){


this.number = number; number = 10;
} super.number = 20;
}
public int getNumber(){ public int getNumber(){
return number; return number;
} }
} }

public class Driver{


public static void main(String args[]){ > 10
Child c = new Child();
c.methodChild();

System.out.println(c.getNumber()); If it’s overridden,


} the method will
} access child field
first
Non-Access Modifier Final
To limit the access to modify the class, field, or
method
Final modifier in method
– Method cannot be overridden by child class, for use only
– public final void finMethod(){ … }
public class Parent{ public class Child extends Parent{
public void publicMethod(){ public void publicMethod(){
//ok
} }
final public void finMethod(){ public void finMethod(){
// forbidden
} }
} }
Non-Access Modifier Final
Final modifier in class
– Class cannot be extended (subclassed) by other class
– public final class FinClass{ … }

Public final class FinalParent{ public class Child extends FinalParent{


// forbidden
} }
Non-Access Modifier Final
Final modifier in field/variable
– Field/variable is constant, value set when declared or
initiated in constructors, and cannot be changed afterward
– private final double finDouble
public class Driver{
public static void main(String args[]){
final int a = 5;
final int b;
b = 8;

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;

public int getNumber(){ fp1 = p; // forbidden


return number; fp2 = new Parent(); // forbidden
}
} fp2.setNumber(5);
fp1.setNumber(20);
}
}
Constructor is not inherited
If there is only non-default constructor(s) in
parent class
– There must be at least one child constructor that invokes
one parent constructor
– Every constructors in child class must invoke one parent
constructor
– Invoke parent constructor using method super(parameter)

If there is a default constructor in parent class


– the child class does not have to invoke the parent
constructor
Example
public class Parent{ public class Child extends Parent{
protected int vP1; private int vC;
protected double vP2;
public Child(int vC){
this.vC = vC;
public Parent(){ }
}
public void methodChild(){
public void methodParent(){ }
}
} }

If there is a default constructor child class may or


or no constructor at all present may not have its
at parent class, then child own constructor
class does not have to invoke
parent class constructor
Example
public class Parent{ public class Child extends Parent{
protected int vP1; private int vC;
protected double vP2;
public Child(int vC, double vP){
this.vC = vC;
public Parent(int vP1){ super(vP);
this.vP1 = vP1; }
}
public void methodChild(){
public Parent(int vP1,double vP2 ){
}
this.vP1 = vP1;
this.vP2 = vP2; }
}

public Parent(double vP2 ){ If there are non-default


this.vP2 = vP2; constructors in parent class,
} There must be AT LEAST one
child constructor that invokes
public void methodParent(){ one parent constructor
} Use keyword super()
} to invoke parent
constructor
Example
public class Parent{ public class Child extends Parent{
protected int vP1; private int vC;
protected double vP2;
public Child(int vC, double vP){
this.vC = vC;
public Parent(int vP1){ super(vP);
this.vP1 = vP1; }
}
public Child(double vP){
public Parent(int vP1,double vP2 ){
super(5, vP);
this.vP1 = vP1; }
this.vP2 = vP2;
}
public Child(){
super(2.5);
public Parent(double vP2 ){
}
this.vP2 = vP2;
} public void methodChild(){
}
public void methodParent(){ All child constructor must
} invoke
} parent constructor
}
Example
public class Parent{ public class Child extends Parent{
protected int vP1; private int vC;
protected double vP2;
public Child(int vC, double vP){
super(vC, vP);
public Parent(){ }
vP1 = 50;
}
public Child(int vC){
this.vC = vC;
public Parent(int vP1,double vP2 ){
}
this.vP1 = vP1;
this.vP2 = vP2; public Child(){
}
this.vC = 25;
}
public Parent(double vP2 ){
this.vP2 = vP2;
}
ChildmethodChild(){
public void constructors that
If parent class has } did not invoke any
default constructor,
public void methodParent(){ parent constructor will
}
} then the child class may automatically invoke
} or may not invoke the the default parent
parent constructor at all constructor
When to Use
Specialization
– Top-down view

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

For those reasons we can create subclasses that


inherit the base class
When to use
Bottom-Up View
– When there several classes that generally the same (many
methods and fields are the same or similar) but not
necessary that one of the class is to be the child of
another
– To create a module to interacts between classes

For those reasons we can create a parent class


that collects the other classes as its child classes
Benefit of Inheritance
Reusability
– Reuse the methods and data of the existing class

Extends
– Adding new data and new methods to the existing class

Modify
– Modify the existing class by overloading its methods with
your won implementation

Reduction (Grouping and Organizing)


5 things you might find in
an Inheritance Hierarchy:
superclass is too general to declare all behavior, so each
subclass adds its own behavior
superclass legislates an abstract behavior and therefore
delegates implementation to its subclasses
superclass specifies behavior, subclasses inherit behavior
superclass specifies behavior, subclasses can choose to
override behavior completely
– just because a subclass inherits a method doesn’t mean that it must act
in the same way as its superclass
– subclass can choose to reject its superclass’ implementation of any
method and "do it my way"
superclass specifies behavior, subclasses can choose to
override behavior in part
– called partial overriding
Question?
THANK YOU

You might also like