Originally Posted by
helloworld922
In your example code I don't see the super-class/sub-class relationship. What you have are two un-related classes in which one happens to use the other one. In order to have a super-class/sub-class relationship, you must use the
extends keyword:
public class BaseClass
{
// .. code
}
public class SubClass extends BaseClass
{
// ..code
}
The best candidates for practicing inheritance are situations where you can say something is something. For example, consider shapes. A triangle is a shape. A rectangle is a shape. A circle is a shape. The shape class would provide several methods which makes sense for shapes, such as what is the area or perimeter of the shape, and where is the shape.
Side note:
You're directly accessing the fields of the VolcanoRobot class from the VolcanoApplication class. I would recommend that you get into the habit of using getter and setter methods and declare your fields private.
public class Class1
{
private int someVal;
public int getSomeVal()
{
return someVal;
}
public void setSomeVal(int value)
{
someVal = value;
}
}
Oh yes! This is just an Inheritance Hierarchy exercise/concept that is being taught in my book currently, not Superclasses (it merely introduced the IDEA of Superclasses, not the code involved); it's the first chapter hehe. I'll learn about defining Superclasses and such in Chapter 5, sorry for the confusion!
Thanks for the information. It still helps me out for when I get to Chapter 5, and I can apply your example about shapes and such to some Chapter 1 exercises.