Java Inheritance and the super Keyword
Java Inheritance and the super Keyword
Welcome! In this part, we'll focus on inheritance in object-oriented programming (OOP) with Java. Inheritance allows us to share code across classes, thereby enhancing readability and efficiency.
In this lesson, we'll clarify attribute and method inheritance in Java through practical examples. Our lesson's outline includes defining inheritance, exploring attribute inheritance, understanding method inheritance, and decoding the super keyword in Java. Ready? Let's get started!
Defining Inheritance
Inheritance in Java involves creating a subclass that inherits attributes and methods from a superclass. In Java, many scenarios allow classes to share common attributes or methods, making inheritance very useful.
The extends keyword is used to set up inheritance, allowing one class to inherit properties and methods from another class. Here's an example featuring a superclass named Vehicle and a subclass named Car:
In the Car class, the super() constructor within its constructor calls the Vehicle class's constructor, ensuring that inherited properties are initialized correctly. The extends keyword indicates that Car is a subclass of Vehicle.
Our focus in this lesson is primarily on single inheritance, where one superclass is extended by a single subclass.
Attribute Inheritance
