Java Inheritance Notes
Inheritance in Object-Oriented Design
Definition and Purpose:
- Inheritance is an essential object-oriented design technique.
- It allows the creation and organization of reusable classes, promoting software reuse.
Key Concepts:
- Parent Class (Superclass/Base Class): The existing class from which a new class is derived.
- Child Class (Subclass): The newly derived class that inherits properties from the parent class.
- Is-a Relationship: Proper inheritance establishes an is-a relationship, indicating that the child class is a more specific version of the parent class.
- Visualization of inheritance is often represented using UML class diagrams with a solid arrow pointing to the parent class.
Deriving Subclasses
Syntax:
- In Java, the keyword
extendsis used to define inheritance.
public class Car extends Vehicle { // class contents }- This defines
Caras a subclass ofVehicle, inheriting its properties and methods.
- In Java, the keyword
Advantages:
- A derived class can add new methods/variables or override inherited methods.
- Reduces redundancy, leveraging existing software components.
The Protected Modifier
Visibility Modifiers:
- Private: Only accessible within the class it is declared in.
- Public: Accessible from anywhere but may violate encapsulation principles.
- Protected: Accessible in the child class and classes in the same package as the parent class.
- Provides a balance between encapsulation and accessibility.
UML Representation: Protected members are denoted with a
#symbol in UML diagrams.
The Super Reference
Purpose:
- Constructors are not inherited in Java but can be called using the
superreference. - The
superreference is used to invoke the parent class's constructor and access its methods and variables.
- Constructors are not inherited in Java but can be called using the
Usage Example:
public class Dictionary2 extends Book2 {
private int definitions;
public Dictionary2(int numPages, int numDefinitions) {
super(numPages); // Call to parent constructor
definitions = numDefinitions;
}
}
Multiple Inheritance
Overview:
- Java allows only single inheritance, meaning a derived class can have only one parent class.
- This limitation avoids complexities such as name collisions that are inherent in multiple inheritance scenarios.
- Multiple inheritance is not generally needed and adds unnecessary complexity to class design.
Applications of Single Inheritance:
- Encourages a clear and manageable hierarchy in class design.
Example Code Snippets:
- Words.java: Demonstrating method inheritance.
public class Words {
public static void main(String[] args) {
Dictionary webster = new Dictionary();
System.out.println("Number of pages: " + webster.getPages());
System.out.println("Number of definitions: " + webster.getDefinitions());
System.out.println("Definitions per page: " + webster.computeRatio());
}
}
Output:
- Number of pages: 1500
- Number of definitions: 52500
- Definitions per page: 35.0
Book.java and Dictionary.java: Examples of class hierarchy and the use of the protected modifier.