Browse By Unit
3 min readβ’june 18, 2024
Avanish Gupta
Avanish Gupta
The Big Takeaway Of This Unit
**Object-Orientated Programming Inheritance and polymorphism are two central pillars of object-orientated programming and refer to some of the ways abstraction is attained. **
After learning about classes with the first two principles of object-orientated programming and different data structures, it's time to learn about the last two principles of object-orientated programming:Β inheritanceΒ andΒ polymorphism. Inheritance allows us to have classes that share the properties of another class. Polymorphism includes allowing an object to be called by both its class and the "parent class" as well.
In this unit, you'll learn how to use inheritance and polymorphism to make different classes. Using these, you'll reduce the code you would have to write otherwise with multiple unrelated classes. Once you learn about inheritance and polymorphism, you will know most of what you need to know for real-life object-orientated programming and can code most real-world situations!
In this unit, we will mainly focus on the last two principles of object-orientated programming. In the first half of the unit, we will discussΒ inheritance, while in the second half, we will discuss polymorphism. Inheritance is where one class, called theΒ subclass, can share methods and instance variables with another class called theΒ superclass.Β
When we make a subclass, the subclass can use all of the methods and instance variables of the superclass with the exact same implementation without having to write these again. However, we will learn of the exception to this in Topic 9.3. The subclass can also add its own methods and instance variables that are specific to the subclass.
When we use inheritance, we think of a subclass as a more specific type of the superclass. For example, let's create the superclass SchoolSubject, which contains objects like math, science, and PE, but we have a subclass called APSubject, which is a subclass of SchoolSubjects and contains objects like apCSA and apBio. Because of inheritance, all APSubject objects are also SchoolSubject objects, but this isn't true the other way around. We will discuss this more in Topic 9.5.
A subclass can only inherit from one superclass. This is because of theΒ diamond problemΒ in programming. Here is how it works:
Making subclasses in Java is very straightforward. To make a subclass, we just add the wordsΒ extends SuperClassName to the class header of the subclass. Let's do an example. Suppose a class A is a superclass for class B, which itself is a superclass for class C, the superclass of classes D and E. Let's write the class headers of the five classes:
public class A {
}
public class B extends A {
}
public class C extends B {
}
public class D extends C {
}
public class E extends C {
}
<< Hide Menu
3 min readβ’june 18, 2024
Avanish Gupta
Avanish Gupta
The Big Takeaway Of This Unit
**Object-Orientated Programming Inheritance and polymorphism are two central pillars of object-orientated programming and refer to some of the ways abstraction is attained. **
After learning about classes with the first two principles of object-orientated programming and different data structures, it's time to learn about the last two principles of object-orientated programming:Β inheritanceΒ andΒ polymorphism. Inheritance allows us to have classes that share the properties of another class. Polymorphism includes allowing an object to be called by both its class and the "parent class" as well.
In this unit, you'll learn how to use inheritance and polymorphism to make different classes. Using these, you'll reduce the code you would have to write otherwise with multiple unrelated classes. Once you learn about inheritance and polymorphism, you will know most of what you need to know for real-life object-orientated programming and can code most real-world situations!
In this unit, we will mainly focus on the last two principles of object-orientated programming. In the first half of the unit, we will discussΒ inheritance, while in the second half, we will discuss polymorphism. Inheritance is where one class, called theΒ subclass, can share methods and instance variables with another class called theΒ superclass.Β
When we make a subclass, the subclass can use all of the methods and instance variables of the superclass with the exact same implementation without having to write these again. However, we will learn of the exception to this in Topic 9.3. The subclass can also add its own methods and instance variables that are specific to the subclass.
When we use inheritance, we think of a subclass as a more specific type of the superclass. For example, let's create the superclass SchoolSubject, which contains objects like math, science, and PE, but we have a subclass called APSubject, which is a subclass of SchoolSubjects and contains objects like apCSA and apBio. Because of inheritance, all APSubject objects are also SchoolSubject objects, but this isn't true the other way around. We will discuss this more in Topic 9.5.
A subclass can only inherit from one superclass. This is because of theΒ diamond problemΒ in programming. Here is how it works:
Making subclasses in Java is very straightforward. To make a subclass, we just add the wordsΒ extends SuperClassName to the class header of the subclass. Let's do an example. Suppose a class A is a superclass for class B, which itself is a superclass for class C, the superclass of classes D and E. Let's write the class headers of the five classes:
public class A {
}
public class B extends A {
}
public class C extends B {
}
public class D extends C {
}
public class E extends C {
}
Β© 2024 Fiveable Inc. All rights reserved.