0% found this document useful (0 votes)
6 views17 pages

Oop Lec 08

The document provides an overview of method overriding, the final keyword, and the super keyword in Java. It explains that final methods, variables, and classes cannot be overridden or extended, while the super keyword is used to access parent class members and constructors. Additionally, it includes code snippets to illustrate these concepts and their usage in Java programming.

Uploaded by

sjadhfwefqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

Oop Lec 08

The document provides an overview of method overriding, the final keyword, and the super keyword in Java. It explains that final methods, variables, and classes cannot be overridden or extended, while the super keyword is used to access parent class members and constructors. Additionally, it includes code snippets to illustrate these concepts and their usage in Java programming.

Uploaded by

sjadhfwefqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Method overriding, final

and super keyword

JAVA

Instructor Name: Syed Shahzad Hassan


Department of Computer Science, HITEC University Taxila - Pakistan
Contents
2

 Recap of previous lecture


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

• 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
4

• The Java final keyword can be used in many contexts.


• The final keyword in Java is used to restrict the user.
• Final can be:

• Variable
• Method
• Class
Final Keyword (Variable)
5

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()
{
speedlimit=400;
Example }
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}}
//Output: Compile time error
Final Keyword (Method)
6

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

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
8

The super keyword in Java is a reference variable which is


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

Use of super with variables: This scenario occurs when a derived class
and base class have 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
10

/* 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
11

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
12

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 Child 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
13

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 and
non-parametric constructors depending on the situation.
Following is the code snippet to explain the above concept:
Super Keyword uses
14

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
15

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.
References
16

• 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-java
• https://www.javatpoint.com/method-overriding-in-java
• https://www.geeksforgeeks.org/super-keyword/
17

THANK YOU

You might also like