Unit 3 - Lecture-2
Unit 3 - Lecture-2
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:
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 :
//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 .
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.