Superclasses and Subclasses.
Definition:
A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors. The term "superclass" refers to a class's direct ancestor as well as to all of its ascendant classes.Often an object of one class "as in" object of another class as well. A rectangle is a quadrilateral. Thus, class Rectangle can said to inherit from the class Quadrilateral. In this context, class Quadrilateral is a superclass and class Retangle is a subclass.
Superclass SubClass
Shape Circle
Triangle
Rectangle
Inheritance Diagram
We can always treat a Circle as a Shape but cannot treat a Shape as a Circle.
Constructors
Example Constructor (Student extends Person)
public Student(String aName, int aYear) {
super(aName); // call superclass (Person) constructor with name
classYear = aYear; } // assignment year to Student class
}
Terminology:
A superclass is sometimes called the base class
A subclass is sometimes called the derived class.
A superclass is sometimes called the parent class.
A subclass is sometimes called the child class
Extends
We use the extends keyword to create a subclass
Example:
public class Student extends Person
Protected Members
A superclass's public members are accessible anywhere the program has a reference to that superclass type or one of its subclass types. A superclass's private members are accessible only in methods of that superclass.
Note private methods are not inherited.
Using the "this" reference
Sometimes there is ambiguity when referring to the classes' data members. When we want to use the current object's data members we can explicitly do this with the this keyword.
When a method of a class references another member of that class for a specific object of that class, how does Java ensure that the proper object is referenced? The answer is that each object has a reference to itself - called the this reference.
Explicitly using this can increase program clarity in some contexts in which this is optional.
Examples:
public int getHour() {return this.hour;} // explicitly (and optionally) referring to the classes data members.
public Time setHour(int hour) { this.hour = hour}; // need to reference class's members.
Super
When a subclass method overrides a superclass method, the supeclass method may be accessed from the subclass by preceding the superclass method with the keyword super followed by the dot operator.
Overloading vs. Overriding
What is the difference between overriding and overloading?
Overloading is part of polymorphism, using the same method (function) name with a different set of parameters. We often see this in constructors, so there are multiple ways to create an object.
Overriding occurs when the subclass has the same method with the same parameters as the superclass. The subclass's method overrides the superclass's method.
Exercise:
You should read through the tutorial on Inheritance You should be able to answer the first two questions.
http://www.holtsoft.com/java/resources/inherit_tutorial/Inheritance/Inheritance.html
Assignment 3.
Create a class called TurtleDash that inherits from the Turtle but which draws all lines as interrupted (dashed) lines rather than continuous lines. Use the class to instantiate 3 turtles to draw three primary colours: blue, yellow and red. Start each turtle at a different spot on the screen then have each make 25 moves of distance 25 pixels randomly in 4 directions (left, right, up or down). Have them move "simultaneously", that is, the blue one makes a move, then the yellow then the red.