A subclass inherits all public methods from its superclass, and these methods remain public in the subclass. But, we also usually add more methods or instance variables to the subclass. Sometimes, we want to modify existing inherited methods. This is called overriding methods.
Overriding an inherited method means providing a public method in a subclass with the same method signature (method name, parameter type list and return type) as a public method in the superclass. The method in the subclass will be called instead of the method in the superclass. One method that is frequently overridden is the toString method. The example below shows a similar method called greet.
To override an inherited method, the method in the child class must have the same name, parameter list, and return type (or a subclass of the return type) as the parent method. Any method that is called must be defined within its own class or its superclass.
You may see the @Override annotation above a method. This is optional but it provides an extra compiler check that you have matched the method signature exactly.
@Override
public String greet()
{
return "Go Away";
}
Activity5.3.1.
Add another subclass called SpanishGreeter (or another language that you know) that extends Greeter and override the greet() method to return Hola! (or hi in another language) instead of Hi!. Create an object to test it out.
Donβt get overriding a method confused with overloading a method! Overloading a method is when several methods have the same name but the parameter types, order, or number are different. So with overriding, the method signatures look identical but they are in different classes, but in overloading, only the method names are identical and they have different parameters.
// overriding methods
g2.greet(); // This could be calling an overridden greet method in g2's class
g1.greet("Sam"); // This calls an overloaded greet method
In the example below the greet(String who) method overloads the greet() method of Greeter. Notice that MeanGreeter inherits this method and it isnβt overridden.
To overload a method the method must have the same name, but the parameter list must be different in some way. It can have a different number of parameters, different types of parameters, and/or a different order for the parameter types. The return type can also be different but you canβt have two methods that differ only in their return type.
public class Person
{
private String name = null;
public Person(String theName)
{
name = theName;
}
public String getFood()
{
return "Hamburger";
}
}
public class Student extends Person
{
private int id;
private static int nextId = 0;
public Student(String theName)
{
super(theName);
id = nextId;
nextId++;
}
public int getId()
{
return id;
}
public void setId(int theId)
{
this.id = theId;
}
}
public class Person
{
private String name = null;
public Person(String theName)
{
name = theName;
}
public String getFood()
{
return "Hamburger";
}
}
public class Student extends Person
{
private int id;
private static int nextId = 0;
public Student(String theName)
{
super(theName);
id = nextId;
nextId++;
}
public int getId()
{
return id;
}
public void setId(int theId)
{
this.id = theId;
}
}
What happens if you change the main method in the Java Visualizer to create a new Student object instead of a Person object? Does it still print the same thing?
Inheritance means that an object of the child class automatically includes the instance variables and methods defined in the parent class. But if the inherited instance variables are private, which they should be, the child class can not directly access the them using dot notation. The child class can use public accessors (also called getters or get methods) which are methods that get instance variable values and public mutators (also called modifier methods or setters or set methods) which set their values.
For example, if a parent class has a private instance variable, name, then the parent will often provide a public getName method and a public setName method as shown below.
class Item
{
private int x;
public void setX(int theX)
{
x = theX;
}
// ... other methods not shown
}
public class EnhancedItem extends Item
{
public int y;
public void setY(int theY)
{
y = theY;
}
// ... other methods not shown
public static void main(String[] args)
{
EnhancedItem currItem = new EnhancedItem();
// missing code
}
}
Even though an EnhancedItem object will have a x field the subclass does not have direct access to a private field. Use the public setX method instead.
The following Pet class keeps track of a petβs name and type and has a constructor, get method, and a method called speak() that prints an animal noise.
Override the method speak() in the Dog class to print out a barking sound like Woof!. (Do not override the get method. This superclass method should work for all subclasses).
Write a similar Cat class that inherits from Pet and has a similar constructor with type cat and overrides the method speak() with a Meow!. Test it out.