Home > Java > javaTutorial > How Can I Access Subclass Methods from a Superclass in Java?

How Can I Access Subclass Methods from a Superclass in Java?

Barbara Streisand
Release: 2024-12-07 10:04:12
Original
707 people have browsed it

How Can I Access Subclass Methods from a Superclass in Java?

Calling Subclass Methods from Superclass

Problem

In Java, when inheriting classes and creating subclasses, it's common to encounter issues accessing subclass-specific methods from within the superclass. This occurs when you instantiate a subclass object and assign it to a superclass variable.

Solution

To resolve this limitation, you can use either of the following methods:

  • Declare the variable as the derived class:
Cat cat = new Cat("Feline", 12, "Orange");
cat.getName(); // OK
cat.getColor(); // OK (getColor() is in Cat)
Copy after login
  • Cast the variable to the desired concrete type:
Pet pet = new Cat("Feline", 12, "Orange");
((Cat)pet).getName(); // OK
((Cat)pet).getColor(); // OK (explicitly treated as Cat)
Copy after login

When you perform a cast, you temporarily treat the object as an instance of the specified type. This allows you to access subclass-specific members and methods.

Example

Consider the following modified Main class:

public class Kennel {
    public static void main(String[] args) {
        // Create the pet objects
        Cat cat = new Cat("Feline", 12, "Orange");
        Pet dog = new Dog("Spot", 14, "Dalmatian");
        Pet bird = new Bird("Feathers", 56, 12);

        // Print out the status of the animals
        System.out.println("I have a cat named " + cat.getName()
                + ". He is " + cat.getAge() + " years old."
                + " He is " + cat.getColor()
                + ". When he speaks he says " + cat.speak());

        // Using a cast to access a subclass-specific method
        ((Cat)dog).getBreed(); // dog is treated as Cat to access getBreed()
        System.out.println("I also have a dog named " + dog.getName()
                + ". He is " + dog.getAge() + " years old."
                + " He is a " + ((Cat)dog).getBreed()
                + ". When he speaks he says " + dog.speak());

        System.out.println("And Finally I have a bird named "
                + bird.getName() + ". He is " + bird.getAge() + " years old."
                + " He has a wingspan of " + bird.getWingspan() + " inches."
                + " When he speaks he says " + bird.speak());
    }
}
Copy after login

In this example, the Main class successfully retrieves the breed of the dog using a cast.

The above is the detailed content of How Can I Access Subclass Methods from a Superclass in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template