OOPS Module 3 Notes
OOPS Module 3 Notes
MODULE 3
Inheritance:
Inheritance Basics: Inheritance is a mechanism in OOP that enables a new class to
inherit characteristics (fields and methods) from an existing class.
It forms a hierarchical relationship between classes, promoting code reuse and
establishing a hierarchy of related classes.
A class that inherits from another class can reuse the methods and fields of that class.
In addition, we can add new fields and methods to your current class as well.
Need for Inheritance
In Java, inheritance serves several important purposes, making it a crucial feature in
object-oriented programming. Some key reasons highlighting the need for inheritance
in Java include:
Code Reusability:
Elimination of Redundancy: Inheritance allows subclasses to inherit
properties and behaviors from a superclass. This reduces redundant
code by inheriting common attributes and methods from a shared
parent class.
Enhancing Software Design:
Hierarchy Creation: Inheritance helps in creating a hierarchy of
classes, organizing them in a logical manner based on their
relationships. This promotes a more understandable and maintainable
code structure.
Extensibility and Maintenance:
Adding New Functionality: Subclasses can extend the functionality of
the superclass by adding new methods or fields without modifying the
original class. This allows for easy extension and modification of code.
Polymorphism:
Method Overriding: Inheritance facilitates polymorphism, where
subclasses can provide their own implementation of methods inherited from
the superclass. This enables the same method name to behave differently
based on the object it is called upon.
Flexibility and Modularity:
Customization and Specialization: Subclasses can specialize and
customize behaviors inherited from the superclass to suit specific needs. This
allows for the creation of more specialized and specific classes.
Software Development Efficiency:
Faster Development: Reusing existing code through inheritance
accelerates the development process by leveraging already established and tested
code.
// Subclass - Car
class Car extends Vehicle {
// Inherits attributes/methods from Vehicle
// Can have additional attributes/methods specific to cars
}
// Subclass - Bike
class Bike extends Vehicle {
// Inherits attributes/methods from Vehicle
// Can have additional attributes/methods specific to bikes
}
Key Points:
The superclass provides a common set of characteristics to its subclasses.
Subclasses inherit properties from their superclass and can extend their functionality
by adding new attributes or methods.
Inheritance establishes an "IS-A" relationship, meaning a subclass "IS-A" type of its
superclass.
Understanding the relationship between a superclass and its subclasses is crucial in
leveraging inheritance to create a hierarchy of related classes and promote code reuse in
object-oriented programming.
Using super
In Java, the super keyword is used to refer to the superclass, primarily within subclasses, and
serves various purposes. Here are the key usages of the super
1. Accessing Superclass Members:
Accessing Superclass Fields and Methods:
Allows a subclass to access methods or fields defined in its superclass.
Helps in cases where a method or field in a superclass is hidden by a similar
member in the subclass.
Example
class Superclass {
int num = 10;
void display() {
System.out.println("Superclass method");
}
}
class Subclass extends Superclass {
int num = 20;
void display() {
System.out.println("Subclass method");
}
void accessSuperclassMembers() {
System.out.println(super.num); // Accessing superclass field
super.display(); // Invoking superclass method
}
}
Method Overriding
Method overriding is a key concept in object-oriented programming that allows a subclass to
provide a specific implementation for a method that is already defined and declared in its
superclass. It enables the subclass to provide its own version of a method that has the same
name, return type, and parameters as a method in the superclass. Here's a detailed explanation
of method overriding:
Key Points:
Inheritance and Overriding:
Method name, parameters, and return type must be the same in both superclass and
subclass.
The access modifier of the overriding method should not be more restrictive than the
overridden method (e.g., if the superclass method is public, the overriding method
cannot be private).
The overridden method in the superclass must not be final, as final methods cannot be
overridden.
class A{
int i,j;
voidshowij(){
Systen.out.println(“ i and j:” + i + “ ” + j);
}
}
class B extends A{
int k;
void showk() {
System.out.println(“k: ” +k);
}
void sum(){
System.out.println(“i+j+k: ” +(i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}