4.1 Inheritance Basics
Inheritance is a fundamental mechanism in Java by which one class (the child or subclass) acquires the properties (fields) and behaviors (methods) of another class (the parent or superclass). It represents an IS-A relationship (e.g., a Dog IS-A Animal).
- Superclass (Parent): The class whose features are inherited.
- Subclass (Child): The class that inherits the other class. It can add its own fields and methods in addition to the superclass fields and methods.
- Syntax: The extends keyword is used to inherit a class.
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks: Woof!");
}
}
4.2 Inheritance Types
Java supports several types of inheritance at the class level:
- Single-level Inheritance: One subclass inherits from exactly one superclass.
- (Class A ← Class B)
- Multi-level Inheritance: A subclass is derived from another subclass, forming a chain.
- (Class A ← Class B ← Class C)
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
- (Class A ← Class B, Class A ← Class C)
- Multiple Inheritance (Not Supported via Classes): Java does not support multiple inheritance with classes to prevent the "Diamond Problem" (ambiguity when two parent classes have methods with the same exact signature). However, multiple inheritance can be achieved using Interfaces.
4.3 'super' Keyword
The super keyword is a reference variable used to refer to immediate parent class objects. It is utilized in three primary contexts:
- Invoke parent class constructor: super() can be used to call the constructor of the parent class. It must be the very first statement inside the child class constructor.
- Invoke parent class methods: Used when child classes have overridden a parent method, but the child needs to execute the parent's logic. (e.g., super.display())
- Access parent class variables: Used to access a variable of the parent class when both the child and parent have variables with the same name (hiding).
class Parent {
int age = 50;
Parent() { System.out.println("Parent Constructor"); }
void printAge() { System.out.println("Parent Age: " + age); }
}
class Child extends Parent {
int age = 20;
Child() {
super(); // Calls Parent()
System.out.println("Child Constructor");
}
void printAge() {
super.printAge(); // Calls parent's printAge()
System.out.println("Child Age: " + age);
System.out.println("Accessing Parent's variable: " + super.age);
}
}
4.4 Polymorphism: Method Overloading and Method Overriding
Polymorphism means "many forms." In Java, it is the ability of an object or method to take on many forms. It is divided into two types:
1. Compile-Time Polymorphism (Method Overloading)
Achieved when a class has multiple methods with the same name but a different parameter list (different number, type, or order of parameters). It is resolved by the compiler during compile-time.
2. Run-Time Polymorphism (Method Overriding)
Achieved when a subclass provides a specific implementation for a method that is already defined in its superclass. The method must have the exact same name, return type, and parameters. It is resolved by the JVM at runtime (Dynamic Method Dispatch).

FeatMethod Overloading
Method Overriding
4.5 Object Class
In Java, the Object class is the supreme root of the entire class hierarchy. By default, every class in Java directly or indirectly inherits from the java.lang.Object class. It provides several built-in foundational methods:
- toString(): Returns a string representation of the object. Often overridden to display meaningful state data.
- equals(Object obj): Compares the memory addresses of the objects. Often overridden to compare the actual values (state) of the objects.
- hashCode(): Returns an integer hash code value for the object, supporting hash-based data structures like HashMap.
- clone(): Creates and returns a copy of the object.
4.6 'final' Keyword
The final keyword is a non-access modifier used to impose restrictions on a class, method, or variable.
- Final Variable: Creates a constant. Its value cannot be reassigned once initialized. (e.g., final double PI = 3.14159;)
- Final Method: Prevents the method from being overridden by any subclasses.
- Final Class: Prevents the class from being inherited completely. Security classes like String are declared final.
4.7 Abstract Class and Methods
Abstraction is the process of hiding implementation details and showing only the essential features to the user.
- Abstract Class: Declared using the abstract keyword. It cannot be instantiated (cannot create objects of it using new). It can have both abstract and non-abstract (concrete) methods.
- Abstract Method: A method declared with the abstract keyword but without a body (no implementation). Subclasses that inherit the abstract class must provide the implementation for all abstract methods, or else they must also be declared abstract.
abstract class Shape {
// Abstract method (no body)
abstract void draw();
// Regular method
void display() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
// Providing implementation for the abstract method
void draw() {
System.out.println("Drawing a Circle...");
}
}
4.8 Access Control
Access modifiers restrict the visibility and accessibility of classes, methods, and variables. Java provides four levels of access control:
Modifier
4.9 Interface: Defining, Implementing, and Applying
An Interface is a blueprint of a class that can only contain constants and abstract methods (prior to Java 8). Interfaces provide a way to achieve 100% abstraction and support Multiple Inheritance in Java.
- Defining: Use the interface keyword. Methods are implicitly public abstract, and variables are implicitly public static final.
- Implementing: A class uses the implements keyword to inherit an interface. It must provide the logic for all abstract methods defined in the interface.
- Applying: Interfaces are heavily used to establish contracts for classes. They ensure that different classes implementing the same interface provide specific capabilities.
// Defining an Interface
interface Printable {
void print(); // inherently public abstract
}
interface Showable {
void show();
}
// A class implementing multiple interfaces (Multiple Inheritance)
class Document implements Printable, Showable {
public void print() {
System.out.println("Printing document...");
}
public void show() {
System.out.println("Showing document on screen...");
}
}
Note: Since Java 8, interfaces can also contain default methods (with bodies) and static methods to allow backward compatibility when adding new methods to interfaces.