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

Unit 3 - Lecture-2

This document discusses inheritance, polymorphism, and the final keyword in Java. It explains that constructors in derived classes automatically call the no-argument constructor in the base class. Final variables, methods, and classes are explained - final variables cannot be changed, final methods cannot be overridden, and final classes cannot be extended. The use of final is described to improve performance in Java programs.

Uploaded by

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

Unit 3 - Lecture-2

This document discusses inheritance, polymorphism, and the final keyword in Java. It explains that constructors in derived classes automatically call the no-argument constructor in the base class. Final variables, methods, and classes are explained - final variables cannot be changed, final methods cannot be overridden, and final classes cannot be extended. The use of final is described to improve performance in Java programs.

Uploaded by

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

Object Oriented Programming

Prof. Rahul B. Diwate

1
Unit-3
• Inheritance:
• Inheritance in Java,
• Types,
• Constructor in Inheritance,
• Using final with Inheritance,
• Accessing superclass member,
• Override private methods,
• Parent and Child classes having same data member,
• Base vs derived class reference.
• Polymorphism:
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding,
• Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Constructors in Derived Classes
• In Java, constructor of base class with no argument gets automatically called in
derived class constructor. For example, output of following program is:

• Base Class Constructor Called


• Derived Class Constructor Called
Keyword extends is used to declaring a global
variable or function in another file to provide the
reference of variable or function

But, if we want to call parameterized


constructor of base class, then we can call
it using super(). The point to note is base
class constructor call must be the first
line in derived class constructor.
Final Classes and methods
• Final keyword in java is used in various contexts:

A) Final Variable
B) Final Method
C) Final Classes
Final Variable
 Final variables are variables that cannot be changed i.e. constant. It is really
good practice to use final variables using uppercase letters and underscores as
separator. Something as like below :

• Final variables are written in capital letters.


EX. final int MIN=1;
• Final variable are declared in 3 ways:
• Final variables cannot be modified.
EX. final int MY_VARIABLE = 10;
• MY_VARIABLE cannot be changed after this initialization.
• If you want to change, you will get compile time error.
class Car
{
final int speedlimit=90;//final variable
void run()
{
int speedlimit=400;
System.out.println(speedlimit);
}
public static void main(String args[])
{
Car obj=new Car();
obj.run();
}
}
Final Method
• Similar to final variables, we can have final method. Means method that cannot be
changed.
• Behavior of a method can only be changed by overriding it in another class.
• So, final methods are not allowed to override.
class Test
{
final public void show() class Final_Method
{ {
System.out.println("hello there"); public static void main(String
}} args[])
class Test1 extends Test {
{ Test t=new Test1();
public void show() t.show();
{ }}
System.out.println("hello i am there");
}}
//Car.JAVA
public class Car {
final int WHEELS = 4;
public final int getNoOfWheels(){
return WHEELS;
} }

//Van.JAVA
public class Van extends Car {
}

//Main.JAVA
public class Main {
public static void main(String[] args){
Van van = new Van();
System.out.println("no of wheels of a Van "+van.getNoOfWheels());
}}
Explanation
• In the above example we have one Car class with one final method getNoOfWheels()
that returns 4 .
• We have created one new class ‘Van’ extending ‘Car’ . In ‘Main’ class, we are able
to access the final method ‘getNoOfWheels’ from ‘van’ object. i.e. it is inheriting the
method from its parent class.
• But if we try to override it inside ‘Van’ class, one compile-time error will be thrown
mentioning that a final method cannot be overriden
Final Class
• Final class is a class that cannot be extended i.e. it cannot be inherited. e.g. Int and Float are
final classes .

public final class Final_Class {


final int WHEELS = 4;
public final int getNoOfWheels(){
return WHEELS;
}
}

Now, if we try to create another class by extending class ‘Car’, it will show one compile time error
message.
final class Bike{}
class Honda1 extends Bike
{
void run()
{ System.out.println("running safely with 100kmph");
} }
public class Final_Class
{ public static void main(String args[])
{ Honda1 honda= new Honda1();
honda.run();
} }
Final Method & Final Class
• Final methods cannot be overridden.
• Final methods can be inherited but not overridden.
• Final class cannot be extended.
• Final class can be instantiated.
• Final keyword can be applied to variable, method and class in Java.
• Final variables cannot be changed, final methods cannot be override and final
class cannot be extended.
• Final variables should be initialized always. At the time of declaration, inside
constructor, inside static method (for static final variables ) or inside instance
initializer block.
• A constructor cannot be final
• All variables declared inside interface are final
• Using final variables, methods and classes in Java improves performance.
• Final keyword can be applied to variable,method and class in Java.
• Final variables cannot be changed, final methods cannot be override
and final class cannot be extended.
• Final variables should be initialised always. At the time of declaration,
inside constructor, inside static method (for static final variables ) or
inside instance initializer block.
• A constructor cannot be final
• All variables declared inside interface are final
• Using final variables, methods and classes in Java improves
performance.

You might also like