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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:36:03+00:00 2026-06-13T10:36:03+00:00

So I have this strucutre – an interface called Animal : public interface Animal

  • 0

So I have this strucutre – an interface called Animal :

public interface Animal {

    public String move ();

    public String makeSound();

    public String getType();

}

then an abstract class called AbstractBird which implements Anima:

public abstract class AbstractBird implements Animal {
    public String birdType;
    public String getType() {
        return birdType;
    }
    @Override
    public String move() {
        return "Fly";
    }
}

then a few classes that extends AbstractBird with the same stucture and called like Dove, Hawk, Eagle etc.. and look like this:

public class Eagle extends AbstractBird {
    public Eagle() {
        birdType = "Eagle";
    }

    @Override
    public String makeSound() {
        return "Noone knows";
    }
}

Then is my class AnimalSound which has the main method :

public class AnimalSound {
    public static void main(String[] args) {
        Eagle e = new Eagle();
        Dove d = new Dove();
        Hawk h = new Hawk();
        play(e);
        play(d);
        play(h);
    }
    public static void play(Animal a) {
        System.out.println("````````````");
        System.out.println(a.getType());
        System.out.println(a.makeSound());
        System.out.println(a.move());

    }
}

As you can see I have this method getType() which returns the birdtype. The method itself is implemented in the abstract class which is OK, but still to get the correct birType I need to write constructor for each class where I can init birdType and this is the part the I don’t like much. So my question is how can I implement the getType() method in the abstract class so it returns the name of the class and/or the name of the object. I’m not sure which would be better in my example the classes have the name of the birds, but thinking of it I think that maybe there’s more sense to return to name of the object.

  • 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-06-13T10:36:05+00:00Added an answer on June 13, 2026 at 10:36 am

    @Leron

    here is an example:

    class Untitled {
      private static abstract class Animal {
        public String getType() {
          return this.getClass().getSimpleName();
        }
      }
    
      private static class Bird extends Animal {
    
      }
    
      private static class Dog extends Animal {
    
      }
    
      public static void main(String[] args) {
        Animal bird = new Bird();
        System.out.println(bird.getType()); // prints Bird
    
        Animal dog = new Dog();
        System.out.println(dog.getType()); // prints Dog
      }
    }
    

    As a result you will get proper type names and you do not need to implement constructors in each derived type. Late binding will solve this problem for you.

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

Sidebar

Related Questions

I have this structure of classes: public class L3Message { public int Number {
I have this structure: <div class=father> This string is left-aligned <div class=divToCenter style=display:inline-block;> //contains
I have this structure: 1) main activity: public class mainActivity extends Activity { @Override
I have a class with this structure: public class BusinessObject { public int Column5
I have this structure `UserInterface.php` abstract class User implements UserInterfacee in model` `class student
I have this structure: ///Creep.java/// public interface Creep extends Movable<Position2D> { ... } ///Movable.java///
I have this structure in my program: class Node0 private Server server; ... public
I have this structure: public class User { public ObjectId Id { get; set;
I have this structure on form, <input type=test value= id=username /> <span class=input-value>John Smith</span>
Suppose i have this structure of elements: <div class=parent> <div class=something1> <div class=something2> <div

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.