Oop Lec 08
Oop Lec 08
JAVA
• The overriding method must have same return type (or subtype)
• 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
class Bike{
final void run(){System.out.println("running");}
}
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
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
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