Inheritance#
Overview#
Definition
Inheritance is a mechanism by which a new class (subclass or derived class) inherits properties (fields and methods) and behaviors from an existing class (superclass or base class). The subclass can extend or modify the behavior of the superclass.
Use Cases
Creating specialized classes that share common attributes and behaviors.
Extending the functionality of existing classes without modifying their code.
Implementing polymorphism, allowing objects of different classes to be treated as objects of a common superclass.
Pros & Cons
Modularity
Classes can be organized hierarchically, aiding in code management.
Reusability
Reusing existing code reduces redundancy and development time.
Efficiency
By inheriting methods and fields, new classes can leverage existing functionality.
Polymorphism
Subclasses can be treated as instances of their superclass, allowing dynamic method invocation.
Tight Coupling
Changes to the superclass can affect its subclasses, leading to unintended consequences.
Inheritance Hierarchy Complexity
Deep hierarchies can be intricate and challenging to manage.
Fragile Base Class Problem
Modifications to the base class can inadvertently break subclasses.
class inheritance
Syntax#
class Animal:
method speak() // Common method, to be overridden
class Dog extends Animal:
method speak()
output "Woof!"
class Cat extends Animal:
method speak()
output "Meow!"
1#include <iostream>
2
3class Animal {
4public:
5 virtual void speak() {
6 std::cout << "Some sound..." << std::endl;
7 }
8};
9
10class Dog : public Animal {
11public:
12 void speak() override {
13 std::cout << "Woof!" << std::endl;
14 }
15};
16
17class Cat : public Animal {
18public:
19 void speak() override {
20 std::cout << "Meow!" << std::endl;
21 }
22};
23
24int main() {
25 Animal *a;
26 Dog d;
27 Cat c;
28
29 a = &d;
30 a->speak(); // Output: Woof!
31
32 a = &c;
33 a->speak(); // Output: Meow!
34
35 return 0;
36}
1class Animal:
2 def speak(self):
3 print("Some sound...")
4
5class Dog(Animal):
6 def speak(self):
7 print("Woof!")
8
9class Cat(Animal):
10 def speak(self):
11 print("Meow!")
12
13a = Animal()
14d = Dog()
15c = Cat()
16
17a.speak() # Output: Some sound...
18d.speak() # Output: Woof!
19c.speak() # Output: Meow!
1class Animal {
2 void speak() {
3 System.out.println("Some sound...");
4 }
5}
6
7class Dog extends Animal {
8 void speak() {
9 System.out.println("Woof!");
10 }
11}
12
13class Cat extends Animal {
14 void speak() {
15 System.out.println("Meow!");
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 Animal a = new Animal();
22 Dog d = new Dog();
23 Cat c = new Cat();
24
25 a.speak(); // Output: Some sound...
26 d.speak(); // Output: Woof!
27 c.speak(); // Output: Meow!
28 }
29}
1trait Animal {
2 fn speak(&self);
3}
4
5struct Dog;
6
7impl Animal for Dog {
8 fn speak(&self) {
9 println!("Woof!");
10 }
11}
12
13struct Cat;
14
15impl Animal for Cat {
16 fn speak(&self) {
17 println!("Meow!");
18 }
19}
20
21fn main() {
22 let d = Dog;
23 let c = Cat;
24
25 d.speak(); // Output: Woof!
26 c.speak(); // Output: Meow!
27}
1package main
2
3import "fmt"
4
5type Animal interface {
6 Speak()
7}
8
9type Dog struct{}
10
11func (d Dog) Speak() {
12 fmt.Println("Woof!")
13}
14
15type Cat struct{}
16
17func (c Cat) Speak() {
18 fmt.Println("Meow!")
19}
20
21func main() {
22 var a Animal
23
24 d := Dog{}
25 c := Cat{}
26
27 a = d
28 a.Speak() // Output: Woof!
29
30 a = c
31 a.Speak() // Output: Meow!
32}
Output
Python & Java
Some sound...
Woof!
Meow!
C++, Rust, and Golang
Some sound...
Woof!
Meow!
Comparing & Contrast#
Language |
Inheritance Syntax |
Overriding Method |
Polymorphism |
---|---|---|---|
C++ |
|
|
Yes |
Python |
|
|
Yes |
Java |
|
|
Yes |
Rust |
|
Trait implementation |
Yes |
Go |
|
Method implementation |
Yes |