Inheritance and Subclasses | Super | Constructors | Abstract Classes and Methods | References and Objects | Casting | Overriding Methods |
Because we have developed new relations for classes other than the distinction of outside class, we need to define a new visibility modifier. Now we have three categories:
BaseClass public class BassClass { : : }DerivedClass public class DerivedClass extends BassClass { : : }
High.java public class High { public High() {} public void print() { System.out.println("high"); } }Middle.java public class Middle extends High { public Middle() {} public void print() { System.out.print("middle "); super.print(); } }Low.java public class Low extends Middle { public Low() {} public void print() { System.out.print("low "); super.print(); } }
Main.java public class Main { public static void main(String[] args) { (new High()).print(); (new Middle()).print(); (new Low()).print(); } }output high middle high low middle high
Note that, just like the keyword this, we call it like a method name for constructors but use it as an object reference for a scope when calling any other members.
XYPoint.java public class XYPoint { protected int x; protected int x; public XYPoint(int x, int y) { this.x = x; this.y = y; } }XYPoint.java public class XYZPoint { protected int z; public XYPoint(int x, int y, int z) { super(x,y); this.z = z; } }
AbstractWanderer.java public abstract class AbstractWanderer { protected Location location; protected boolean direction; public abstract void move(); protected int random(int low, int high) { return (int)(Math.random()*(high-low+1))+low; } protected int booleanToInt(boolean b) { if (b) return 1; else return -1; } public String toString() { return "A Wanderer at " + location; } }NorthSouthWanderer.java public class NorthSouthWanderer extends AbstractWanderer { public void move() { int dx = random(-1,1); int dy = booleanToInt(direction); location.moveOneSpace(dx,dy); } }EastWestWanderer.java public class EastWestWanderer extends AbstractWanderer { public void move() { int dx = booleanToInt(direction); int dy = random(-1,1); location.moveOneSpace(dx,dy); } }
Animal.java
public class Animal { protected void makeNoise() { System.out.println("Don't know what noise to make."); } }
Dog.java
public class Dog extends Animal { public void makeNoise() { bark(); } public void bark() { System.out.println("bark"); }
object tpye Animal Dog reference type Animal Animal aa = new Animal();
aa.makeNoise(); Animal ad = new Dog();
ad.makeNoise();Dog Dog da = new Animal();
da.makeNoise();da.bark(); illegal Dog dd = new Dog();
dd.makeNoise();dd.bark();
Main.java public class Main { public static void main(String[] args) { Animal aa = new Animal(); try { ((Dog)aa).bark(); } catch (ClassCastException cce) { System.out.println("the object to which " + "aa referes is not a Dog!"); } } }console output the object to which aa referes is not a Dog!
Main.java public class Main { public static void main(String[] args) { Animal ad = new Dog(); if (ad instanceof Dog) { ((Dog)ad).bark(); } } }console output bark
main class calling Animal.makeNoise() calling Dog.makeNoise() Main.java public class Main { public static void main(String[] args) { Animal ad = new Dog(); ad.makeNoise(); } }==> console output Don't know what noise to make.or console output bark?
Collection.java public abstract class Collection { private int size; public Collection() { size = 0; } public int size() { return size; } public void add(Object data) { ++size; } public abstract Object access(int index); public String toString() { StringBuffer buffer = new StringBuffer("<"); for (int i = 0; i < size(); ++i) { buffer.append(" " + access(i)); if (i < size() - 1) buffer.append(","); } buffer.append(" >"); return buffer.toString(); } }FastAccessCollection.java public class FastAccessCollection extends Collection { private Object[] array; public FastAccessCollection() { super(); array = new Object[size()]; } public void add(Object data) { super.add(data); Object[] biggerArray = new Object[size()]; biggerArray[0] = data; for (int i = 1; i < size(); ++i) { biggerArray[i] = access(i-1); } array = biggerArray; } public Object access(int index) { return array[index]; } }
FastAddCollection.java public class FastAddCollection extends Collection { private final ListNode first; public FastAddCollection() { super(); first = new ListNode(null); } public void add(Object data) { super.add(data); ListNode dataNode = new ListNode(data); dataNode.setNext(first.getNext()); first.setNext(dataNode); } public Object access(int index) { ListNode marker = first; int i = 0; while (i <= index) { marker = marker.getNext(); ++i; } return marker.getData(); } class ListNode { private ListNode next; private final Object data; ListNode(Object data) { this.data = data; next = null; } public Object getData() { return data; } public ListNode getNext() { return next; } public void setNext(ListNode next) { this.next = next; } } }
Test.java public class Test { public static void main(String[] args) { Collection c = new FastAccessCollection(); c.add(new Integer(5)); c.add(new Integer(4)); c.add(new Integer(3)); c.add(new Integer(2)); c.add(new Integer(1)); System.out.println(c.access(3)); } }Test.java public class Test { public static void main(String[] args) { Collection c = FastAddCollection(); c.add(new Integer(5)); c.add(new Integer(4)); c.add(new Integer(3)); c.add(new Integer(2)); c.add(new Integer(1)); System.out.println(c.access(3)); } }