You are on page 1of 17

INHERITANCE

INHERETANCE
Is An Technique Used To Inherit Attributes And
Methods From One Class To Another
SUPER CLASS

The Class Where We Will Inherit The Attributes


And Methods
SUB CLASS

The Class Who Will Inherit The Attributes And


Method From A Super Class
EXTENDS KEYWORD

Used After The Class Name And It Indicates


That The Certain Class Will Inherit From
Another Class
USING INHERITANCE
Extends Keywords After Subclass Name
Modifiers class subClassName extends superClassName {
//attributes & methods
}
USING INHERITANCE
Extends Keywords After Subclass Name
class person { class toddler extends person{

String name, sex ; void drink(){


Int age; System.out.println(“drinking milk”)

}
//methods

}
OVERRIDING CONSTRUCTOR
OVERRIDING CONSTRUCTOR
You are required to call the constructor of the
superclass
class person { class toddler extends person{

Person (arguments){ toddler(arguments){


Super(arguments);
//constructor //add attributes

} }
}
SUPER KEYWORD
Super keyword can only be used by a subclass
and it is used to call their superclass so we can
access their constructors, attributes and
methods
OVERRIDING METHOD
To retain functionality from the super class use the
super keyword with the method name
class person { class toddler extends person{

Void checkstatus(){ Void checkstatus(){


Super.checkStattus();
//output attributes //add functionalities
}
}
}
POLYMORPHISM
POLYMORPHISM
“poly” means MANY and “morph” means TAKE
DIFFERENTS FORMS

It is an oop technique that utilizes inheritance to create


1 class and make several classes inherit from that
class so it can take many forms
USING POLYMORPHISM
class dog extends animal{
class animal {
Void makesound(){
Void makesound(){
//arf
//sound
}
} }
}
class pig extends animal{

Void makesound(){

//oink

}
}
ACTIVITY
Apply this technique (polymorphism) on your own ..
Create any class that could act as the superclass and
make subclasses that will inherit from that super class

You might also like