Re: Inheritance and lousy Frame
NickName wrote:
package oop1;
abstract public class MammalClass {
// members variable definition
private String name, eyeColor;
private int age;
public static void main(String[] args) {
};
This declaration of a main() serves no purpose here.
// Accessor methods
// name property
public String getName() {
return name;
}
public void setName(String value) {
name = value;
}
Methods like this should be declared "final" to prevent a subclass override,
if they are to be used in a constructor.
// eyeColor property
public String getEyeColor() {
return eyeColor;
}
public void setEyeColor(String value) {
eyeColor = value;
}
// age property
public int getAge() {
return age;
}
public void setAge(int value) {
if (value > 0) {
age = value;
}
else {
age = 0;
}
}
// provide default value
public MammalClass() {
setName("some name");
setEyeColor("dark");
setAge(10);
Because if the subclass overrides these methods, you could have some pain in
the superclass constructor.
System.out.println("test output from super class");
}
// abstract class, abstract method; declare at the supper class level
but implemented at each subclass
abstract public void speed();
}
// subclass
package oop1;
public class DogClass extends MammalClass{
// class members
// boolean hasTail;
// use the parent's members as well
private boolean Tail;
Variable names should begin with a lower-case letter.
public boolean hasTail() {
return Tail;
}
public void setTail(boolean value) {
Tail = value;
}
// calling super class's Accessor methods
public DogClass() {
setName("Pal");
setAge(3);
This would actually invoke the subclass's methods if there were such
overrides. Depending on the subclass's method definitions, that could break
your code.
Putting non-final public methods in a constructor is dangerous.
// test output
System.out.println("My dog: " + getName());
System.out.println("is + " + getAge() + "now.");
}
public void speed() {
javax.swing.JOptionPane.showMessageDialog(null, "30 mph", "Dog
Speed", 1);
Not sure that suddenly throwing a Swing class into a console app is such a
good idea.
}
}
- Lew