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

Module-3

The document discusses key concepts of inheritance and polymorphism in Java, highlighting the use of superclass variables to reference subclass objects, the 'super' keyword, method overriding, and abstract classes. It explains the benefits of inheritance, such as code reusability and runtime polymorphism, and introduces the final keyword to prevent inheritance. Additionally, it covers dynamic method dispatch for runtime method resolution based on object type.

Uploaded by

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

Module-3

The document discusses key concepts of inheritance and polymorphism in Java, highlighting the use of superclass variables to reference subclass objects, the 'super' keyword, method overriding, and abstract classes. It explains the benefits of inheritance, such as code reusability and runtime polymorphism, and introduces the final keyword to prevent inheritance. Additionally, it covers dynamic method dispatch for runtime method resolution based on object type.

Uploaded by

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

Module-III

Inheritance and polymorphism


A Superclass variable can reference a subclass object,
Using super, Method Overriding, Using Abstract classes,
Final with inheritance, Abstract Classes and Dynamic
Method Dispatch.
Inheritance

• Java, Inheritance is an important pillar


of OOP(Object-Oriented Programming).
It is the mechanism in Java by which
one class is allowed to inherit the
features(fields and methods) of another
class. In Java, Inheritance means
creating new classes based on existing
ones. A class that inherits from another
class can reuse the methods and fields
of that class. In addition, you can add
new fields and methods to your current
class as well.
Why Do We Need Java Inheritance?

• Code Reusability: The code written in the


Superclass is common to all subclasses. Child
classes can directly use the parent class code.
• Method Overriding: Method Overriding is
achievable only through Inheritance. It is one
of the ways by which Java achieves Run Time
Polymorphism.
• Abstraction: The concept of abstract where we
do not have to provide all details, is achieved
through inheritance. Abstraction only shows
the functionality to the user.
How to Use Inheritance in Java?
• The extends keyword is used for inheritance in
Java. Using the extends keyword indicates you are
derived from an existing class. In other words,
“extends” refers to increased functionality.

• Syntax :

• class DerivedClass extends BaseClass


• {
• //methods and fields
• }
// Java Program to illustrate Inheritance
• import java.io.*;

• // Base or Super Class


• class Employee {
• int salary = 60000;
• }

• // Inherited or Sub Class


• class Engineer extends Employee {
• int benefits = 10000;
• }

• // Driver Class
• class Inherit {
• public static void main(String args[])
• {
• Engineer E1 = new Engineer();
• System.out.println("Salary : " + E1.salary + "\nBenefits : " + E1.benefits);
• }

Superclass Variable Reference Subclass Object
Key Points:
In Java, a superclass variable can hold a reference to a subclass object.
This is an example of polymorphism.
Allows using a superclass type to reference subclass objects dynamically.
Example Code:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Animal myDog = new Dog(); // Superclass reference to subclass object
myDog.sound(); // Outputs: Dog barks
Using super

Key Points:
The super keyword is used to call the superclass's methods and constructors.
super can be used to refer to the superclass's hidden fields and methods.
Helpful in method overriding when the subclass needs to call the superclass version.
Example Code:
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(color); // prints color of Dog class
System.out.println(super.color); // prints color of Animal class
}
}
Dog d = new Dog();
d.printColor();
Method Overriding

Key Points:
Method overriding allows a subclass to provide a specific implementation of a
method already defined in its superclass.
The method in the subclass should have the same name, return type, and
parameters.
Used for runtime polymorphism and to implement specific behavior in a
subclass.
Example Code:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
Animal myCat = new Cat();
myCat.sound(); // Outputs: Cat meows
Using Abstract Classes
Key Points:
Abstract classes cannot be instantiated directly.
They can have abstract methods (without a body) and concrete methods (with
implementation).
Subclasses must implement all abstract methods unless the subclass is also
abstract.
Example Code:
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Lion extends Animal {
void sound() {
System.out.println("Lion roars");
}
}
Lion lion = new Lion();
lion.sound(); // Outputs: Lion roars
lion.sleep(); // Outputs: Animal is sleeping
Final with Inheritance

Key Points:
The final keyword can be used to prevent inheritance of a class.
A final method cannot be overridden by subclasses.
Useful for classes and methods that are critical for security or
intended to remain unchanged.
Example Code:
final class FinalClass {
final void finalMethod() {
System.out.println("This is a final method.");
}
}
// The following code will cause a compile-time error
// class SubClass extends FinalClass {} // Error: Cannot inherit
from final class
Abstract Classes and Dynamic Method
Key Points: Dispatch
Dynamic method dispatch is the mechanism by which a call to an overridden
method is resolved at runtime.
An abstract class can be used to define common behavior, and subclasses can
provide specific implementations.
Useful for designing systems where the exact type of objects might not be known
until runtime.
Example Code:
abstract class Animal { abstract void sound(); }
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}}
Animal myAnimal; myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks
myAnimal = new Cat();
myAnimal.sound(); // Outputs: Cat meows
Summary

Key Takeaways:
A superclass reference can point to a subclass object, enabling
polymorphism.
The super keyword allows access to superclass members and constructors.
Method overriding enables subclasses to provide specific implementations
of superclass methods.
Abstract classes provide a framework for subclasses, ensuring they
implement specific methods.
Final keyword prevents further inheritance and method overriding.
Dynamic method dispatch facilitates runtime method resolution based on
object type.
Questions & Answers
Thank You!

You might also like