3 min read•Last Updated on 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 {
}
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 12
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 12
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 12
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Object-oriented programming (OOP): OOP is a programming paradigm that uses abstraction as one of its core principles. It enables developers to model real-world entities as objects with well-defined behaviors and attributes.
Interface: An interface defines a contract for how a class should behave, specifying a set of methods that must be implemented. It allows for abstraction by separating the definition from the implementation, enabling different classes to provide their own implementations.
Polymorphism: Polymorphism refers to the ability of an object to take on many forms. In programming, it allows objects of different classes to be treated as instances of a common superclass or interface, providing flexibility and extensibility.
Classes are user-defined data types in object-oriented programming. They serve as blueprints for creating objects by defining their attributes and methods.
Objects: Objects are instances created from classes. They represent individual entities with their own unique characteristics.
Inheritance: Inheritance is a mechanism in which one class inherits properties (attributes and methods) from another class, allowing for code reuse.
Encapsulation: Encapsulation is the concept of bundling data (attributes) and methods together within a class to hide implementation details from outside access.
Data structures are ways to organize, store, and manipulate data efficiently. They provide different methods of accessing, inserting, deleting, or searching for data elements based on specific requirements.
Arrays: Arrays are fixed-size collections that store elements of the same type sequentially in memory. They offer fast access but limited flexibility in terms of resizing.
Linked Lists: Linked lists consist of nodes connected through pointers where each node holds a value and a reference to the next node. They allow dynamic memory allocation but have slower access times compared to arrays.
Stacks: Stacks follow the Last-In-First-Out (LIFO) principle where elements can only be inserted or removed from one end called the top. They are useful for tracking function calls or undo/redo operations.
Superclasses refer to classes that other classes inherit from. They contain common attributes and behaviors that can be shared by multiple subclasses.
Inheritance: This is a concept in object-oriented programming where one class inherits properties and methods from another class.
Polymorphism: This refers to the ability of objects belonging to different classes to respond differently based on their own implementation of shared methods.
Encapsulation: This is an object-oriented programming principle that involves bundling data and methods together within a class, providing control over access and modification.
Subclasses are classes that inherit properties and behaviors from superclasses. They can add additional attributes or override existing ones to create specialized versions of the superclass.
Overriding: This is a concept in object-oriented programming where a subclass provides its own implementation for a method that is already defined in the superclass.
Polymorphism: As mentioned earlier, polymorphism allows objects belonging to different classes, including subclasses, to respond differently based on their own implementation of shared methods.
Abstract Classes: These are classes that cannot be instantiated but serve as blueprints for other classes. Subclasses must implement any abstract methods defined in the abstract class.
Static typing is a programming language feature that requires variables to be declared with their data types before they can be used. It helps catch errors at compile-time by enforcing type compatibility.
Dynamic Typing: In contrast to static typing, dynamic typing allows variables to hold values of different types during runtime.
Type Inference: Type inference is a feature in some programming languages where the compiler automatically determines the data type of a variable based on its assigned value.
Strong Typing: Strong typing refers to a programming language's ability to prevent operations between incompatible data types, ensuring type safety.
The object superclass is the root class in Java and many other object-oriented programming languages. All classes are derived from this superclass either directly or indirectly.
Subclass: A subclass is a class that inherits properties and behaviors from another class (superclass).
Inheritance: Inheritance is an object-oriented concept where one class (subclass) derives properties and behaviors from another class (superclass).
Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and code reusability.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.
The diamond problem refers to an issue that arises in multiple inheritance when two superclasses share a common superclass, resulting in ambiguity for subclasses on which superclass's method to use.
Multiple Inheritance: Multiple inheritance is when a subclass inherits characteristics and behaviors from more than one superclass.
Superclass/Subclass Relationship: The relationship between classes where one class (subclass) inherits properties and behaviors from another (superclass).
Method Overriding: Method overriding occurs when a subclass provides its own implementation for a method that is already defined in its superclass.