Let's say I have my classic:
public abstract class Mammal {
private int numLegs;
private String voice;
private Coat coat;
public abstract void eat();
public abstract void speak();
public abstract void sleep();
private abstract void ingestFood(Food f); //The abstract method ingestFood in type Mammal can only set a visibility modifier, one of public or protected
}
With these concrete implementations:
public class Dog extends Mammal {
private int numLegs = 4;
private String voice = "Woof!";
private Coat coat = new Coat(COATTYPE.FUR, COATCOLOR.BROWN);
@Override
public void eat()
{
Rabbit r = chaseRabbits();
if (r != null) ingest(r);
else {
Garbage g = raidBin();
if (g!= null) ingest(g);
}
}
@Override
private void ingest(Food f)
{
gobbleItAllUpInFiveSeconds(f);
stillFeelHungry();
}
}
public class Human extends Mammal {
private int numLegs = 2;
private String voice = "Howdy!!";
private Coat coat = new Coat(COATTYPE.SKIN, COATCOLOR.PINK);
@Override
public void eat()
{
Food f = gotoSuperMarket();
ingest(f);
}
@Override
private void ingest(Food f)
{
burp();
}
}
Now, I want a method in the Mammal
class that is callable by all instances of the mammal, e.g.
public String describe() {
return "This mammal has " + this.numLegs + "legs, a " + this.coat.getColor() + " " this.coat.getCoatType() + " and says " + this.voice;
}
My question is that, by making the Mammal
class not abstract, is it possible to create a mammal by itself? E.g.
Mammal me = new Mammal();
You shouldn't be able to do this.
However, I do want to have some public methods that are implemented by the parent class that all subclasses call, but that each call their own private method.