Data Structures using Java Part 10: Inheritence, Constructors, invoking orverriden methods, protected keyword, dynamic method lookup, Abstract class


Inheritence
————-
TailList is a subclass of SList.
SList is the superclass of TailList.

A subclass can modify a superclass in three different ways:
1. It can declare new fields.
2. It can declare new methods.
3. It can override old methods with new implementations. It is not necessary to orverride all.

public class TailList extends SList{

	// the head and size is inherited

	private SListNode tail;

	// override the insertEnd method
	public void insertEnd(Object obj){
	// solution
	}

}

Inheritence and Constructors
——————————–
Java executes the TailList constructor, but first it executes SList() constructor.

public TailList(){
	//SList constructor is called

	//additional
	Tail = null;
}

Zero parameter constructor is called by default. To change:

public TailList(int x){
	super(x); // calls the super class constructor and must be executed FIRST.
	tail = null;
}

Invoking overridden methods
——————————-

public void insertFront(Object obj){
	super.insertFront(obj); // for the superclass and  not necessarly the first line.
	if(size == 1){
	tail = head;
	}
}

The “protected” keyword
—————————-

public class SList{

	protected SListNode head;
	protected int size;
}

Both “protected ” field and method is visible to the declaring class and to all its subclasses.
In contrast, “private ” fields are not visible to subclasses.

Dynamic method lookup
————————
For Java “Every TailList is an SList”.

SList s = new TailList(); // TailList is a subclass of SList and SList is not abstract
s.insertFront(obj);

Abstract class
—————-
A class whose sole purpose it to be inherited. This is an ADT. An abstract method lacks the implementation.

public abstract class List{

	protected int size;

	public int lenght(){
		return size;
	}

	public abstract void insertFront(Object obj);

}

List mylist;
mylist = new List(); //compile time error

A non abstract class may never
1. contain an abstract method.
2. inherit one without providing an implementation.

List mylist = new SList(); // correct!
mylist.insertFront(obj);

One list sorter can sort every kind of list.

public void listSort(List l){
//code
}

Subclasses of List: SList, DList,TailList
TimedList: record amount of time doint list operations
TransactionList: Logs all the changes on a disk for power outage.

Data Structures using Java Part 10: Inheritence, Constructors, invoking orverriden methods, protected keyword, dynamic method lookup, Abstract class