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!
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
:
Java1// Define the superclass 'Vehicle' 2class Vehicle { 3 // Initialize the Vehicle with color and brand attributes 4 private String color; 5 private String brand; 6 7 public Vehicle(String color, String brand) { 8 this.color = color; 9 this.brand = brand; 10 } 11 12 public String getColor() { 13 return color; 14 } 15 16 public String getBrand() { 17 return brand; 18 } 19} 20 21// Define the subclass 'Car', inheriting from 'Vehicle' 22class Car extends Vehicle { 23 private int doors; 24 25 public Car(String color, String brand, int doors) { 26 super(color, brand); 27 this.doors = doors; 28 } 29 30 public int getDoors() { 31 return doors; 32 } 33} 34 35public class Solution { 36 public static void main(String[] args) { 37 Car myCar = new Car("Red", "Toyota", 4); 38 System.out.println("Color: " + myCar.getColor()); // Output: Color: Red 39 System.out.println("Brand: " + myCar.getBrand()); // Output: Brand: Toyota 40 System.out.println("Doors: " + myCar.getDoors()); // Output: Doors: 4 41 } 42}
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 allows a subclass to inherit the attributes of its superclass, except for private fields, which are not directly accessible in subclasses.
Consider this example featuring a superclass named Artist
, and a subclass named Musician
:
Java1class Artist { 2 private String name; 3 4 public Artist(String name) { 5 this.name = name; 6 } 7 8 public String getName() { 9 return name; 10 } 11} 12 13class Musician extends Artist { 14 private String instrument; 15 16 public Musician(String name, String instrument) { 17 super(name); 18 this.instrument = instrument; 19 } 20 21 public String getInstrument() { 22 return instrument; 23 } 24} 25 26public class Solution { 27 public static void main(String[] args) { 28 Musician john = new Musician("John Lennon", "Guitar"); 29 System.out.println(john.getName()); // Output: John Lennon 30 System.out.println(john.getInstrument()); // Output: Guitar 31 } 32}
The Musician
class inherits the name
attribute from the Artist
class and adds its own unique attribute, instrument
. Access to the name
attribute is provided via a getter method since it is private.
Similar to attributes, method inheritance allows a subclass to inherit the methods of its superclass.
In the example below, the Car
class can invoke the start
method from the Vehicle
class:
Java1class Vehicle { 2 private String brand; 3 4 public Vehicle(String brand) { 5 this.brand = brand; 6 } 7 8 public void start() { 9 System.out.println("The " + brand + " is starting."); 10 } 11 12 public String getBrand() { 13 return brand; 14 } 15} 16 17class Car extends Vehicle { 18 public Car(String brand) { 19 super(brand); 20 } 21} 22 23public class Solution { 24 public static void main(String[] args) { 25 Car myCar = new Car("BMW"); 26 myCar.start(); // Output: The BMW is starting. 27 } 28}
In the absence of a specific constructor in the Car
class, Java provides a default constructor that internally calls super()
to ensure the superclass's constructor is invoked. This initialization allows the Car
class to inherit the brand
attribute from the Vehicle
class seamlessly.
The super
keyword is essential in inheritance for calling superclass methods from a subclass, especially useful in method overriding. It enables a subclass to extend or utilize the functionality of a superclass without directly modifying it.
Method overriding in Java allows a subclass to provide a specific implementation of a method already defined in its superclass. This is achieved by using the @Override
annotation in the subclass.
For instance, when overriding a method to add or alter its behavior, super
can be used to call the original method from the superclass and integrate its functionality with new enhancements:
Java1class Vehicle { 2 public String start() { 3 return "Vehicle is starting..."; 4 } 5} 6 7class Car extends Vehicle { 8 @Override 9 public String start() { 10 return super.start() + " Beep! Beep!"; 11 } 12} 13 14public class Solution { 15 public static void main(String[] args) { 16 Car myCar = new Car(); 17 System.out.println(myCar.start()); // Output: Vehicle is starting... Beep! Beep! 18 } 19}
In this way, super
allows subclasses to build upon or adapt the functionality of their superclasses efficiently and cleanly.
We've successfully explored attribute and method inheritance in Java and practiced using several examples. Mastering these concepts in real-life programming can enhance both efficiency and readability. Remember, practice is essential for proficiency!
On that note, are you ready for some practice exercises? They will solidify your understanding and prepare you for more complex programming tasks. Programming is all about experimenting, learning, and problem-solving. Enjoy the journey!