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

Inheritance Part-2

This document discusses method overriding in Java, explaining its concept, usage, and rules, along with examples. It also covers the final and super keywords, detailing their functionalities and restrictions in the context of inheritance. Additionally, it highlights important points regarding constructors and references for further reading.

Uploaded by

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

Inheritance Part-2

This document discusses method overriding in Java, explaining its concept, usage, and rules, along with examples. It also covers the final and super keywords, detailing their functionalities and restrictions in the context of inheritance. Additionally, it highlights important points regarding constructors and references for further reading.

Uploaded by

shahmeasum007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Inheritance Part 2

Method overriding, final


and super keyword

JAVA
Contents
2

 Recap of previous lecture


 Method overriding
 Keywords (final and super)
Recap
3

 What is inheritance, uses, terms and its types


Method Overriding (Concept)
4

If subclass (child class) has the same method as declared in the


parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific


implementation of the method that has been declared by one
of its parent class, it is known as method overriding.
In Java, we can override methods only, not the variables(data members), so runtime polymorphism
cannot be achieved by data members.
Usage of Method Overriding
5

• Method overriding is used to provide the specific


implementation of a method which is already provided
by its superclass.

• Method overriding is used for runtime polymorphism.


Rules for Java Method Overriding
6

• The method must have the same name as in the


parent class

• The method must have the same parameter as in the


parent class.

• There must be an IS-A relationship (inheritance).


Method Overriding (Syntax)
7

class Vehicle
{
void run()
{}
}
class Bike2 extends Vehicle
{
void run()
{}
}
Method Overriding (Example)
8

//Java Program to illustrate the use of Java Method


Overriding
//Creating a parent class.
class Vehicle
{ public static void main(String args[])
//defining a method
{
void run()
{System.out.println("Vehicle is running");} Bike2 obj = new Bike2();//
} creating object obj.run();//
//Creating a child class calling method
class Bike2 extends Vehicle }
{
//defining the same method as in the parent class
void run()
{System.out.println("Bike is running safely");}
}
Method Overriding (Example)
9

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

Java method overriding is mostly used in Runtime Polymorphism which


we will learn in next lecture.
Method Overriding
12

Can we override static method?

No, a static method cannot be overridden. It can be proved by runtime


polymorphism, so we will learn it later.

Why can we not override static method?

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.

Can we override java main method?

No, because the main is a static method.


Method Overriding
13

• Final, static, private methods can not be overridden

• The overriding method must have same return type (or subtype)

• Invoking overridden method from sub-class is possible using super


keyword.

• Overriding and constructor : We can not override constructor as parent


and child class can never have constructor with same name (Constructor
name must always be same as Class name).

For more detail visit this link


https://www.geeksforgeeks.org/overriding-in-java/
Final Keyword
14

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

If you make any method as final, you cannot override it.

class Bike{
final void run(){System.out.println("running");}
}

class Honda extends Bike{


Example void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
} } //Output: Compile time error
Final Keyword (Class)
17

If you make any class as final, you cannot extend it.


final class Bike{}

class Honda1 extends Bike{


void run()
{System.out.println("running safely with 100kmph");}
Example
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
} //Output: Compile time error
Super Keyword
18

The super keyword in Java is a reference variable which is


used to refer immediate parent class object.
Super Keyword uses
19

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

/* Base class vehicle */


class Vehicle
{
int maxSpeed = 120;
/* Driver program to test */
}
class Test
{
/* sub class Car extending vehicle */
public static void main(String[] args)
class Car extends Vehicle
{
{
Car small = new Car();
int maxSpeed = 180;
small.display();
}
void display()
}
{
/* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
Super Keyword uses
21

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

Use of super with constructors: super keyword can also be used to


access the parent class constructor. One more important thing is that,
‘’super’ can call both parametric as well as non parametric
constructors depending upon the situation. Following is the code
snippet to explain the above concept:
Super Keyword uses
24

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

System.out.println("Student class Constructor");


}
}
Other Important points
25

1. Call to super() must be first statement in Derived(Student) Class


constructor.

2. If a constructor does not explicitly invoke a superclass constructor, the Java


compiler automatically inserts a call to the no-argument constructor of the
superclass.

3. If a subclass constructor invokes a constructor of its superclass, either


explicitly or implicitly, you might think that a whole chain of constructors
called, all the way back to the constructor of Object. This, in fact, is the case. It
is called constructor chaining..
References
26

• Absolute JAVA Fifth Edition by Walter Savitch


• https://www.javatpoint.com/inheritance-in-java
• https://www.geeksforgeeks.org/inheritance-in-java/
• https://beginnersbook.com/2013/03/inheritance-in-java/
• https://
www.slideshare.net/AdilAslam4/inheritance-and-its-type-in-ja
va
• https://www.javatpoint.com/method-overriding-in-java
• https://www.geeksforgeeks.org/super-keyword/
27

THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy