Apex Inheritance

Apex Inheritance

Inheritance is one of the most important features of object-oriented programming in Apex. It allows you to create a new class (child class) based on an existing class (parent class), so that the child class can reuse the code (variables and methods) of the parent class without rewriting it. 

This makes your code cleaner, more organized, and easier to maintain. Inheritance also helps in creating a hierarchical relationship between classes — where a general class (like Animal) can be extended by more specific classes (like Dog, Cat, etc.). 

Syntax: 

public class ParentClass {
// Parent variables and methods
}

public class ChildClass extends ParentClass {
// Child-specific variables and methods
}

extends keyword tells Apex that one class is inheriting from another.
The child class can access all public and protected members of the parent class.
Private members are not inherited directly but can be accessed via public methods.

Real-Life Analogy: Inheritance

    Think of a parent and child. 
    A child automatically inherits things like last name, eye color, and traditions from                    their parents — without asking or redefining them. 

Example:

Parent: Owns a house and a car
Child: Gets the house and car without buying them again — they’re inherited.

Just like that, in programming:

Parent class = defines common things
Child class = automatically gets them and can add more of its own

Code Example

1: Basic Inheritance

public class Vehicle {
public void start() {
System.debug(‘Vehicle started’);
}
}

public class Car extends Vehicle {
public void drive() {
System.debug(‘Car is driving’);
}
}

// Using the inherited and own method
Car myCar = new Car();

myCar.start(); // Inherited from Vehicle
myCar.drive(); // Defined in Car

Code Explanation:

Vehicle is the parent class with method start().
Car is the child class that extends Vehicle and adds its own method drive().
myCar is an object of the Car class.
It can access both start() and drive() because it inherits from Vehicle.

Output:

Vehicle started
Car is driving

2: Multiple Child Classes

public class Animal {
public void eat() {
System.debug(‘Animal is eating’);
}
}

public class Cat extends Animal {
public void meow() {
System.debug(‘Cat says Meow’);
}
}

public class Dog extends Animal {
public void bark() {
System.debug(‘Dog says Bark’);
}
}

Cat c = new Cat();
c.eat(); // Inherited
c.meow(); // Own method

Dog d = new Dog();
d.eat(); // Inherited
d.bark(); // Own method

Code Explanation:

Animal is a general (parent) class with a method eat().
Both Cat and Dog extend Animal, inheriting its eat() method.
Each child class also has its own method (meow() or bark()).
Objects of both Cat and Dog can access the parent method and their own.

Output:

Animal is eating
Cat says Meow
Animal is eating
Dog says Bark

3: Inheritance with Constructor

public class Person {
public String name;

public Person(String n) {
name = n;
}

public void greet() {
System.debug(‘Hello, ‘ + name);
}

public class Student extends Person {
public Integer roll;

public Student(String n, Integer r) {
super(n); // Calls Person(n)
roll = r;
}
}


}

Code Explanation:

Person is the parent class with a constructor and a method.
Student is the child class, which uses super(n) to call the parent constructor.
Student adds its own variable roll and method showInfo().
The object s1 can access both inherited and own methods.

Output:

Hello, Adnan Name: Adnan, Roll: 101

Tasks for Practice

Task 1: Create a parent class Shape with a method area(). Create two child classes Circle and Square that inherit from Shape and override the area() method with custom logic.

Task 2: Create a parent class Employee with properties name and salary, and a method displayInfo(). Create a child class Manager that adds a property department and a method showManagerInfo().

Task 3: Write a program where a Parent class has a method sayHello(). A Child class inherits this method and adds a new method sayBye(). Create an object and call both methods.

Task 4: Create a base class Animal with a method makeSound() that prints "Some generic animal sound."

Create a child class Dog that overrides makeSound() to print “Bark!” 
Create a Dog object and call the method.

Task 5: Create a parent class Shape with a method draw(). Then create two child classes:

Circle → draw() prints “Drawing a circle”
Square → draw() prints “Drawing a square” Create objects of each and call draw().