Module-4
Inheritance &
Polymorphism
Dr. S. Vinila Jinny,
ASP/SCOPE.
VIT.
MODULE-4 Inheritance and Polymorphism
Inheritance-types-use of “Super”-final keyword-
Polymorphism-Overloading and Overriding-Abstract
class-Interfaces.
What is Inheritance?
• Inheritance is a mechanism in which one
class acquires the property of another
class.
• For example, a child inherits the traits of
his/her parents.
• With inheritance, we can reuse the fields
and methods of the existing class.
• Hence, inheritance facilitates Reusability
and is an important concept of OOPs.
Types of Inheritance
• Single inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance Multiple inheritance
interface concept
is achieved through
Java Inheritance Syntax:
class subClass extends superClass
{
//methods and fields
}
• “Is-A” relationship
• The parent class is called a super class and
the inherited class is called a subclass.
• The keyword extends is used by the sub
class to inherit the features of super class.
JAVA Inheritance Example
class Doctor {
void Doctor_Details() {
System.out.println("Doctor Details...");
}
}
class Surgeon extends Doctor {
void Surgeon_Details() {
System.out.println("Surgen Detail...");
}
}
public class Hospital {
public static void main(String args[]) {
Surgeon s = new Surgeon();
s.Doctor_Details();
s.Surgeon_Details();
}
}
Super Keyword
• The super keyword is similar to “this”
keyword.
• The keyword super can be used to access
any data member or methods of the
parent class.
• Super keyword can be used at variable,
method and constructor level.
Syntax:
super.<method-name>();
Super Keyword for constructor
• A child’s constructor is responsible for calling the
parent’s constructor otherwise compilation error
• The first line of a child’s constructor should use the
super reference to call the parent’s constructor
Super Keyword for constructor
class Baseclass { public class Derivedclass extends Baseclass {
int age; Derivedclass(int age)
{
Baseclass(int age) super(age);
{ }
this.age = age;
} public static void main(String args[])
{
public void getAge() Derived class s = new Derivedclass(24);
{ s.getAge();
System.out.println("The value of the }
variable named age in super class is: " +age); }
}
}
Super Keyword for Variable & Method
• It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
• If a class is inheriting the properties of another class. And if the
members of the superclass have the names same as the sub class, to
differentiate these variables we use super keyword as shown below.
super.variable
super.method();
Super Keyword for Variable & Method
public void my_method() {
class Super_class { Sub_class sub = new Sub_class();
int num = 20; sub.display();
public void display() { super.display();
System.out.println("This is the display method System.out.println("value of the variable
of superclass"); named num in sub class:"+ sub.num);
} System.out.println("value of the variable
} named num in super class:"+ super.num);
}
public class Sub_class extends Super_class { public static void main(String args[]) {
int num = 10; Sub_class obj = new Sub_class();
public void display() { obj.my_method();
System.out.println("This is the display method }
of subclass"); }
}
Final Keyword
• The final keyword is a non-access modifier used for
classes, attributes and methods, which makes them
non-changeable (impossible to inherit or override).
• The final keyword is useful when you want a variable
to always store the same value, like PI (3.14159...).
• The final keyword is called a "modifier".
Final Keyword
final int number = 10; //final variable
Final Variable final float value; //blank final variable
static final double rate = 2.5; //final and static variable
• We can initialize a blank final variable inside an
instance-initializer block or inside the constructor of
the class. In case, there are many constructors in the
class, then we need to initialize the final variable
inside each constructor, else there will be an error.
• We can initialize a blank final static variable inside a
static block.
Example
public class Main {
final int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 25; // will generate an error: cannot assign a value to a
final variable
System.out.println(myObj.x);
}
}
Final Method
• Declare Java methods as Final Method by adding the
Final keyword before the method name.
• The Method with Final Keyword cannot be overridden
in the subclasses.
Final Class
• Declare a class with a final keyword
• When we declare a class as final, then we restrict
other classes to inherit or extend it.
• Java final class can’t be extended by other classes in
the inheritance. If another class attempts to extend
the final class, then there will be a compilation error.
Polymorphism
• Polymorphism is the ability of an object to take on
different forms.
• “Poly” means numerous, and “Morphs” means
forms.
Types of Polymorphism
• compile-time polymorphism – Method Overloading
• runtime polymorphism – Method Overriding
Run Time Polymorphism-Function Overriding
• In a class hierarchy, when a method in a subclass has
the same name and type signature as a method in its
superclass, then the method in the subclass is said to
override the method in the super class.
Example
class Animal{ class Rabbit extends Animal{
void place(){ void place(){
System.out.println("Animals live on earth."); System.out.println("Rabbit
lives in burrow.");
} }
} }
class Dog extends Animal{ class Polymorphism{
void place(){ public static void main(String[] args) {
System.out.println("Dog lives in kennel."); Animal A = new Animal();
A.place();
} A = new Dog();
} A.place(); Output:
A = new Horse(); Animals live on earth.
class Horse extends Animal{ A.place();
A = new Rabbit();
Dog lives in kennel.
void place(){ Horse lives in stable.
A.place();
System.out.println("Horse lives in stable."); } Rabbit lives in burrow.
} }
}
Upcasting
• Overriding is done by using a reference variable of the
superclass.
• The method to be called is determined based on the
object which is being referred to by the reference
variable. This is also known as Upcasting.
Dynamic Method Dispatch
• Machanism by which a call to an overridden method
is resolved at run time, rather that compile time.
• If a superclass contains a method that is overridden
by a subclass, then when different types of objects
are referred to through a superclass reference
variable, different versions of the method are
executed.
Method Overloading
• Method overloading is the process that can create
multiple methods of the same name in the same
class, and all the methods work in different ways.
• Method overloading occurs when there is more than
one method of the same name in the class.
Example
class Shapes { class Main {
public void area() { public static void main(String[] args) {
System.out.println("Find area "); Shapes myShape = new Shapes();
} myShape.area();
public void area(int r) { myShape.area(5);
System.out.println("Circle area = "+3.14*r*r); myShape.area(6.0,1.2);
} myShape.area(6,2);
public void area(double b, double h) { }
System.out.println("Triangle area="+0.5*b*h); }
} Output:
public void area(int l, int b) { Find area
System.out.println("Rectangle area="+l*b); Circle area = 78.5
}
Triangle area=3.60
}
Rectangle area=12
Abstract Class
• Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
• A class which is declared as abstract is known as an abstract class.
• It can have abstract and non-abstract methods. It needs to be extended and its method
implemented.
• It cannot be instantiated.
Syntax:
• It can have constructors and static methods. abstract class class _name
{
• It can have final methods. }
Abstract Method
• A method which is declared as abstract and does not
have implementation is known as an abstract
method.
Syntax:
abstract void printStatus();
Example
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
Output:
}
running safely
}
Interface
• An interface in Java is a blueprint of a class. It has static
constants and abstract methods.
• The interface in Java is a mechanism to achieve abstraction.
• There can be only abstract methods in the Java interface, not
method body.
• It is used to achieve abstraction and multiple inheritance in
Java.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
Interface
• An interface is declared by using the interface keyword.
• It provides total abstraction; means all the methods in an interface are declared
with the empty body, and all the fields are public, static and final by default.
• A class that implements an interface must implement all the methods declared in
the interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Example
interface printable
{
void print();
} Output:
class my implements printable{ Hello
public void print(){System.out.println("Hello");}
public static void main(String args[]){
my obj = new my();
obj.print();
}
}
Interface achieving Multiple Inheritance
interface Printable{
void print();
}
interface Showable{
void show(); Output:
} Hello
class my implements Printable,Showable{ Welcome
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
my obj = new my();
obj.print();
obj.show();
} }