Implementing Inheritance and Polymorphism
Implementing Inheritance and Polymorphism
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Inheriting a Class
Inheritance signifies the relationship between a
superclass and its subclass. In order to inherit a
class, you need to use the extends keyword.
For example,
if you have a superclass named Books and a
subclass named PaperBooks that share common
functionalities and properties with the
superclass, such as printing the book details and
author name, total number of pages, and price.
Static polymorphism
Dynamic polymorphism
Static Polymorphism
In case of static polymorphism, an entity, such as a
method, can exist in multiple forms. This means that
one or more methods can exist with the same name
but with a different argument list. This type of
polymorphism, when exhibited by methods, is known as
method overloading.
class GameDemo
{
public static void main(String args[])
{
Badminton obj1 = new Badminton();
obj1.playGame();
obj1.computeScore();
obj1.displayScore();
}
}
this keyword
Sometimes, within a method or a constructor,
there may be a need to refer to the object that
has invoked it. Moreover, in your program,
sometimes you may have a requirement where the
class variables and the method or constructor
variables have the same name.
class MainClass
{
public static void main(String args[])
{
Vehicle a = new Vehicle(250);
a.showmaxspeed();
}
}
In the preceding code, the member variable and the
parameter for the constructor of Vehicle share the
same name, max_speed.
In such a scenario, when the object, a, is created, the
value, 250, is assigned to it.
However, when the showmaxspeed() method is invoked,
the value of the class variable, max_speed, is displayed
as 210.
This is because the member variable is hidden by the
parameter of the constructor.
void display()
{
// print maxSpeed of base class (vehicle)
System.out.println("Maximum Speed: "+
super.maxSpeed);
}
}
// Driver Program
class Test
{
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
Output
Maximum Speed: 120
// superclass Person
class Person {
void message()
{
System.out.println("This is person class\n");
}
}
// Subclass Student
class Student extends Person {
void message()
{
System.out.println("This is student class");
}
void display()
{
// will invoke or call current class message()
method
message();
// will invoke or call parent class message() method
super.message();
}
}
class Test {
public static void main(String args[])
{
Student s = new Student();
// calling display() of Student
s.display();
}
}
Output
This is student class
This is person class
// superclass Person
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
// subclass Student extending the Person class
class Student extends Person
{
Student()
{
// invoke or call parent class constructor
super();
// Driver Program
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}
Output
Person class Constructor
Student class Constructor
Syntax:
interface <interface_name>{
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
} //End of Main()
} //End of Class
Multiple inheritance in Java by interface
interface Printable{
void print();
}
class Circle {
public static void main(String[] args) {
// Constant variable to store the value of pi
static final double PI = 3.14;