Lesson 6

Java Inheritance: Mastering Method and Attribute Overriding

Introduction to the Lesson

Welcome back! In this lesson, we're going to explore a critical area of inheritance in Java: overriding methods and attributes. It's akin to taking a classic recipe, the parent class, and tweaking it to suit your unique taste, the child class. Let's begin!

Recap of Inheritance

Inheritance in Java allows a class, referred to as a child, to 'inherit' properties and behaviors from another class, the parent. This is achieved using the keyword extends.

1public class Smartphone extends CellPhone {}

In this example, Smartphone inherits all aspects of CellPhone.

Overriding Methods in Inheritance

Overriding enables a child class to customize inherited methods. When a child class declares a method that is already present in the parent class, we refer to this as method overriding.

Here's an example of how Smartphone overrides a method in CellPhone:

1public class CellPhone { 2 // A method in parent class 3 public void displayBatteryStatus() { 4 System.out.println("Battery status displayed with an icon."); 5 } 6} 7 8public class Smartphone extends CellPhone { 9 // Method overridden in child class 10 @Override 11 public void displayBatteryStatus() { 12 System.out.println("Battery status displayed as a percentage."); 13 } 14}

In this case, Smartphone overrides the displayBatteryStatus method to implement a unique action.

Overriding Attributes in Inheritance

Child classes can also override inherited attributes. For instance, CellPhone possesses an attribute named hasTouchScreen, which is set to false. In contrast, Smartphone changes this attribute as it does feature a touchscreen.

1public class CellPhone { 2 // An attribute in the parent class 3 public boolean hasTouchScreen = false; 4} 5 6public class Smartphone extends CellPhone { 7 // The same attribute overridden in the child class 8 public boolean hasTouchScreen = true; 9}

Here, Smartphone overrides the hasTouchScreen attribute to reflect its unique capabilities accurately.

Understanding 'protected' in Java

The protected identifier in Java serves as a mid-point between private and public. When marked protected, an attribute becomes accessible within all subclasses.

1public class CellPhone { 2 // Protected attribute in parent class 3 protected boolean hasPhysicalKeypad = true; 4} 5 6public class Smartphone extends CellPhone { 7 // The protected attribute overridden in the child class 8 protected boolean hasPhysicalKeypad = false; 9} 10 11CellPhone phone = new CellPhone(); 12System.out.println(phone.hasPhysicalKeypad); // Error! Can't directly access protected field

In this scenario, Smartphone is allowed to access and override the protected attribute hasPhysicalKeypad. At the same time, just directly accessing the same field, or accessing it from a non-inherited class, doesn't work.

Lesson Summary and Practice

Great progress today! You have explored the concepts of method and attribute overriding and discovered the use of protected in Java. Brace yourself for the upcoming exercises, where you will apply these concepts using the CellPhone and Smartphone classes. Let's get started!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.