When a method in a subclass has the same name and type as method in superclass it is known as?

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 common method that is overriden is the toString() method. The example below shows a similar method called greet().

When a method in a subclass has the same name and type as method in superclass it is known as?
Coding Exercise

In the following example the MeanGreeter inherits the greet() method from Greeter, but then overrides it. Run the program to see.

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.

Note

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";
}

10.3.1. Overloading Methods¶

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 overriden greet method in g2's class
g1.greet("Sam"); // This calls an overloaded greet method

When a method in a subclass has the same name and type as method in superclass it is known as?
Coding Exercise

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 overriden.

After running the code, try overriding the greet(String) method in the MeanGreeter class to return “Go away” + the who String.

Note

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.

When a method in a subclass has the same name and type as method in superclass it is known as?
Check your understanding

    10-3-3: Which of the following declarations in Student would correctly override the getFood method in Person?

    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 void getFood()
  • The return type must match the parent method return type.
  • public String getFood(int quantity)
  • The parameter lists must match (must have the same types in the same order).
  • public String getFood()
  • The return type and parameter lists must match.

You can step through an example of this in the Java Visualizer by clicking on the following link Override Example.

When a method in a subclass has the same name and type as method in superclass it is known as?
Check your understanding

    10-3-4: Which of the following declarations in Person would correctly overload the getFood method in Person?

    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 void getFood()
  • You can not just change the return type to overload a method.
  • public String getFood(int quantity)
  • For overloading you must change the parameter list (number, type, or order of parameters).
  • public String getFood()
  • How is this different from the current declaration for getFood?

You can step through an example of this using the Java Visualizer by clicking on the following link Overload Example.

When a method in a subclass has the same name and type as method in superclass it is known as?
Coding Exercise

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?

10.3.2. Inherited Get/Set Methods¶

Inheritance means that an object of the child class automatically includes the object 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 has a private instance variables, name, then the parent typically provides a public getName method and a public setName method as shown below. In the setName method below, the code checks if the passed string is null before it sets it and returns true if the set was successful or false otherwise. The Employee class inherits the name field but must use the public method getName and setName to access it.

Demonstrated inherited get/set methods.

When a method in a subclass has the same name and type as method in superclass it is known as?
Check your understanding

    10-3-6: Given the following class definitions which of the following would not compile if it was used in place of the missing code in the main method?

    class Item
    {
       private int x;
    
       public void setX(int theX)
       {
          x = theX;
       }
       // ... other methods not shown
    }
    
    public class EnhancedItem extends Item
    {
       private 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
       }
    }
    

  • currItem.setX(3);
  • The object currItem is an EnhancedItem object and it will inherit the public setX method from Item.
  • currItem.setY(2);
  • The object currItem is an EnhancedItem object and that class has a public setY method.
  • currItem.x = 3;
  • 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.
  • currItem.y = 2;
  • All code in the same class has direct access to all object fields.

You can step through this code in the Java Visualizer by clicking on the following link Private Fields Example.

10.3.3. Programming Challenge : Pet Sounds¶

When a method in a subclass has the same name and type as method in superclass it is known as?

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.

  1. Write a subclass called Dog that inherits from Pet.

  2. Write a Dog constructor that has one argument, the name, and calls the super constructor passing it the name and the animal type “dog”.

  3. 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).

  4. Uncomment the Dog object in the main method to test it out.

  5. Write a similar Cat class that inherits from Pet and has a similar constructor and overrides the method speak() with a “Meow!”. Test it out.

Complete the Dog and Cat classes below to inherit from Pet with a constructor and a method speak() that prints out “Woof!” or “Meow!”.

10.3.4. Summary¶

  • Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass.

  • Any method that is called must be defined within its own class or its superclass.

  • A subclass is usually designed to have modified (overridden) or additional methods or instance variables.

  • A subclass will inherit all public methods from the superclass (for example all the set and get methods); these methods remain public in the subclass.

  • Overloading a method is when several methods have the same name but the parameter types, order, or number are different.

You have attempted of activities on this page

What happens when superclass and subclass has a methods that have same name?

If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.

When a method in a subclass has the same name and type?

Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.

When a method in a subclass has the same name and type signature as a method in its superclass then in the method in the subclass is said to ?

Static Methods If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

Can subclass have same name superclass?

Subclass methods can call superclass methods if both methods have the same name.