Inheritance Part-2
Inheritance Part-2
JAVA
Contents
2
class Vehicle
{
void run()
{}
}
class Bike2 extends Vehicle
{
void run()
{}
}
Method Overriding (Example)
8
class Human
{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
public static void main( String args[])
}
class Boy extends Human
{
{ Boy obj = new Boy();
//Overriding method //This will call the child class version of eat()
public void eat(){ obj.eat();
System.out.println("Boy is eating"); }
}
Method Overriding (Example)
10
class Bank
{
int getRateOfInterest() {return 0;}
}
//Creating child classes.
class HBL extends Bank
{
int getRateOfInterest() {return 8;}
}
class UBL extends Bank
{
int getRateOfInterest() {return 7;}
}
class ABL extends Bank
{
int getRateOfInterest() {return 9;}
}
Method Overriding (Example)
11
class Test2
{
public static void main(String args[])
{
HBL h=new HBL();
UBL u=new UBL();
ABL a=new ABL();
System.out.println(“HBL Rate of Interest: "+h.getRateOfInterest());
System.out.println(“UBL Rate of Interest: "+u.getRateOfInterest());
System.out.println(“ABL Rate of Interest: "+a.getRateOfInterest());
}
}
It is because the static method is bound with class whereas instance method is
bound with an object. Static belongs to the class area, and an instance belongs
to the heap area.
• The overriding method must have same return type (or subtype)
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
• Variable
• Method
• Class
Final Keyword (Variable)
15
If you make any variable as final, you cannot change the value of
final variable(It will be constant).
class Bike9
{
final int speedlimit=90;//final variable
void run()
{
Example speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}}
//Output: Compile time error
Final Keyword (Method)
16
class Bike{
final void run(){System.out.println("running");}
}
Use of super with variables: This scenario occurs when a derived class
and base class has same data members. In that case there is a
possibility of ambiguity for the JVM. We can understand it more
clearly using this code snippet
Super Keyword uses
20
Use of super with methods: This is used when we want to call parent
class method. So whenever a parent and child class have same named
methods then to resolve ambiguity we use super keyword. This code
snippet helps to understand the said usage of super keyword.
Super Keyword uses
22
class Parent
{
void show()
{
System.out.println("Parent's show()");
} // Driver class
} class Main
// Inherited class {
class Child extends Parent public static void main(String[] args)
{ {
// This method overrides show() of Parent Parent obj = new Child();
@Override obj.show();
void show() }
{ }
super.show();
System.out.println("Child's show()");
}
} Invoking overridden method from sub-class
Super Keyword uses
23
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
/* Driver program to test*/
class Test
/* subclass Student extending the Person class */
{
class Student extends Person
public static void main(String[] args)
{
{
Student()
Student s = new Student();
{
}
// invoke or call parent class constructor
}
super();
THANK YOU