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 4266906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:49:30+00:00 2026-05-21T06:49:30+00:00

Good day! I am reviewing the Java OO concept. And wrote the following codes:

  • 0

Good day!

I am reviewing the Java OO concept.
And wrote the following codes:

  public class Main {  
        public static void main(String[] args) {
            Animal dog = new Dog();
            dog.eat();
            dog.sleep();
        }

    }

abstract public class Animal {

    private int age;
    public Animal (){
        age = 1;
    } 
    public Animal (int age){   //How can I call this constructor?
        this.age = age;
    }
    public void eat(){
        System.out.println("Eat");
    }
    abstract public void sleep();
}

abstract public class Canine extends Animal{

    abstract public void roam();

}

public interface Pet {

    public String petName = null;  //i want the pets to have a variable petName.
    public void trick();
    }


public class Dog extends Canine implements Pet{

    public void roam(){
        System.out.println("roam");
    };

    public void sleep(){
        System.out.println("sleep");
    };

    public void eat(){
        System.out.println("Eat Dog");
    };

    public void trick(){
        System.out.println("trick");
    }

}

I have several questions as follows:

  1. How can I call the Animal Overloaded constructor?

  2. How can I use the variable petName in the PET Interface?

  3. Am I doing the concept of OO correctly? What rules am I violating?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 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-05-21T06:49:31+00:00Added an answer on May 21, 2026 at 6:49 am
    1. Subclasses will call the super constructor from within their own constructor using super(…) as the first line!
    2. Interfaces cannot have variables or state – only methods!
    3. You have a sound concept, but your code would not compile (because of item 2 above).

    Some solutions:

    public interface Pet {
        String getName();
        void trick();
    }
    

    Now the Dog class (or any class that implements Pet) will have to implement Pet.getName(). Give the Dog class a field of type String called ‘name’ and return it from Dog.getName().

    public abstract class Canine extends Animal {
        public Canine(int age) {
            super(age); // pass the age parameter up to Animal
        }
        ...
    }
    
    public class Dog extends Canine implements Pet {
        private final String name;
        public Dog(String name,int age) {
           super(age); // call the age constructor
           this.name=name;
        }
        public String getName() { return name; }
        ... rest of class ...
    }
    

    Each subclass (esp. the abstract ones) will need to provide matching constructors for all parent class constructors you want to call! (So I added the age parameter to the Canine constructor so that Dog could pass an age argument to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I encountered the following error upon running my JSP program. java.lang.IllegalStateException: PWC3991:
Good day. I'm using the WebClient class in my C# application in order to
Good day, I need to extract portion of string which can looks like this:
Good day, I have a hibernate mapping which goes something like this <class name=Person>
Good day, Anybody knows of a good java library for retrieving mime type and
Good day to all. I have the following setup: A page with html, js,
Good day! I've the following link: <%: Url.Action(MVC.Areas.Manage.Feedback.Index()) %> And it is generated as
Good day! I tried using JSTL in java but there's an error: exception javax.servlet.ServletException:
Good day! In his Effective STL Scott Meyers wrote A third is to use
Good day everybody! Having the following code: template<typename T, typename OutStream = std::ostream> struct

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.