News Ticker

Abstract class and Constructor

Question


What is the effect on a child class of writing a constructor in an abstract parent class?

Answer


The answer is…..

Animal.class

[sourcecode language=”java” toolbar=”false”]
public abstract class Animal {

String name;

public Animal(String name) {
this.name = name;
}

abstract boolean born();
abstract boolean die();

public void setColour(){}
}[/sourcecode]

Horse.class

[sourcecode language=”java” toolbar=”false”]
public class Horse extends Animal{

String name;
String colour;

public Horse(String name, String colour){
super(name);
this.name = name;
this.colour = colour;
}

public Horse(String name) {
this(name, "red");
}

boolean born() {
return false;
}

boolean die() {
return false;
}

}[/sourcecode]

Leave a Reply

%d bloggers like this: