Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:14:48+00:00 2026-06-01T09:14:48+00:00

I am in an introductory java course and we just started learning about inheritance.

  • 0

I am in an introductory java course and we just started learning about inheritance. I am working on a task that asks that we create a “Pet” superclass with name and age; and three subclasses, each with their own unique trait (I have chosen “Dog”, “Cat”, and “Bird”). After we have all these built, we are to create a Main class to test everything, and this is where I am running into problems. I am attempting to call the get methods for these unique traits within Main, but it seems to only find methods that are in the superclass.

Here is the Main class:

public class Kennel {
    public static void main(String[] args) {
        // Create the pet objects
        Pet cat = new Cat("Feline", 12, "Orange");
        Pet dog = new Dog("Spot", 14, "Dalmation");
        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());
        System.out.println("I also have a dog named " + dog.getName()
                + ". He is " + dog.getAge() + " years old."
                + " He is a " + 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());       
    }
}

Here is my superclass

abstract public class Pet {
    private String name;
    private int age;

    // Constructor
    public Pet(String petName, int petAge) {
        this.name = petName;
        this.age = petAge;
    }

    // Getters
    public String getName() { return(this.name); }
    public int getAge() { return(this.age); }

    // Setters
    public void setName(String nameSet) { this.name = nameSet; }
    public void setAge(int ageSet) { this.age = ageSet; }

    // Other Methods
    abstract public String speak();

    // toString
    @Override
    public String toString() {
        String answer = "Name: " + this.name + " Age: " + this.age;
        return answer;
    }
}

And here is one of the subclasses (they all look the same and are having the same error)

public class Cat extends Pet {
    private String color;

    // Constructor
    public Cat(String petName, int petAge, String petColor) {
        super(petName, petAge);
        this.color = petColor;
    }

    // Getters
    public String getColor() { return(this.color); }

    // Setters
    public void setColor(String colorSet) { this.color = colorSet; }

    // Other Methods
    @Override
    public String speak() { return "Meow!"; } 

    // toString
    @Override
    public String toString() {
        String answer = "Name: " + super.getName() + " Age: "+super.getAge()
                + " Color: " + this.color;
        return answer;
    }
}

So what is happening is I can’t get the main method to find the cat.getColor() method, or any of the other ones unique to the subclasses.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:14:49+00:00Added an answer on June 1, 2026 at 9:14 am

    When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.

    Pet cat = new Cat("Feline",12,"Orange"); 
    cat.getName(); // this is OK
    cat.getColor(); // this is not OK, getColor() is not in Pet
    

    To access the methods in the concrete class (Cat in this case), you need to either declare the variable as the derived class

    Cat cat = new Cat("Feline",12,"Orange"); 
    cat.getName(); // OK, getName() is part of Cat (and the superclass)
    cat.getColor(); // OK, getColor() is part of Cat
    

    Or cast it to a type you know/suspect is the concrete type

    Pet cat = new Cat("Feline",12,"Orange"); 
    ((Cat)cat).getName(); // OK (same as above)
    ((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat
    

    You can even combine the two methods:

    Pet pet = new Cat("Feline",12,"Orange"); 
    Cat cat = (Cat)pet;
    cat.getName(); // OK
    cat.getColor(); // OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished my exam in an introductory C course about 20 minutes ago.
I'm currently in an introductory level Java class, and am working on the classic
I am currently taking a introductory course in Java and this is regarding try-catch
Some time ago I was a TA in a introductory programming course on Java.
I'm taking an introductory course (3 months) about real time systems design, but any
I am teaching (with others) a relatively introductory course in computer science for IT
I'm following the Django-CMS introductory tutorial and have got everything working up to the
I'm currently learning Python using Zelle's Introductory text, and I'm trying to recreate one
I was making this very simple lex program (just an introductory program). But on
I was reading through introductory articles about windows communication foundation on MSDN and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.