Notes and Exersice for Abstract Class of Completed Java Master Class.
Notes
Abstract class is a normal class but with abstract method.
1 2 3 4 5 6 7 8 9 10 11
public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public abstract void eat(); }
Abstract class can not be instantiated. Abstract method needs to be implemented.
The class extends it have to implement all the abstract classes.
1 2 3 4 5 6 7 8 9 10 11
public class Dog extends Animal{ public Dog(String name) { super(name); }
@Override public void eat() { System.out.println(getName() + " is eating bones.."); } }
Abstract class can be extended by another abstract class
1 2 3 4 5 6 7 8 9 10 11 12 13
public abstract class Bird extends Animal{
public Bird(String name) { super(name); }
@Override public void eat() { System.out.println(getName() + " is eating .."); } public abstract void fly(); }
Need to be implemented by another class so that we can initiate it.
1 2 3 4 5 6 7 8 9 10 11
public class Chicken extends Bird{ public Chicken(String name) { super(name); }
@Override public void fly() { System.out.println(getName() + " is hardly flying.."); } }
main:
1 2 3 4 5 6 7 8 9 10 11 12 13
public class main {
public static void main(String[] args) { Animal myDog = new Dog("funny"); myDog.eat(); //Animal myChicken = new Chicken("fat"); X no fly method Bird myChicken = new Chicken("fat"); myChicken.eat(); myChicken.fly(); } }
Common and Difference between Abstract Class and Interface
Common
Cannot instantiate them. Methods without an implementation
Difference
Abstract class can declare fields that are not static and final
Abstract class can define public, protected and private concrete methods
Abstract class can extend only one parent class but implement multiple interfaces
The purpose of an abtract class is to provide a common definition of a base class that multiple derived classes can share.
Exercise
Requirements
Create own LinkedList class
the item can be added with auto sorting from small to large
the item can be removed
use traverse method to iterate and print all the items
/** * @author luoxu * * Create own LinkedList class * the item can be added with auto sorting from small to large * the item can be removed * use traverse method to iterate and print all the items */
public class SearchTree implements MyList{ /** * the difference: single direction * starting from root to leaf, invalid for reverse * */ private ListItem root = null;
@Override public ListItem getRoot() { return root; }
@Override public boolean addItem(ListItem listItem) { if (root == null) { // empty tree, listItem become root root = listItem; return true; } // otherwise start comparing ListItem currentItem = root; while (currentItem != null) { int comparison = currentItem.compareTo(listItem); if (comparison < 0) { // listItem is greater, move right if possible if (currentItem.next()==null) { // no node to the right, so add at this point currentItem.setNext(listItem); return true; } else { currentItem = currentItem.next(); } } else if (comparison > 0) { // listItem is smaller, move left if possible if (currentItem.previous()==null) { // no node to the left, so add at this point currentItem.setPrevious(listItem); return true; } else { currentItem = currentItem.previous(); } } else { System.out.println("Duplicated item : " + listItem.getValue() ); return false; } } return false; }
private void performRemove(ListItem currentItem, ListItem parentItem) { System.out.println("Deleting:" + currentItem.getValue()); if (currentItem.next()==null) { // no right branch, make parent link to the left branch if (parentItem.next() == currentItem) { // currentItem is the right child of parent parentItem.setNext(currentItem.previous()); } else if (parentItem.previous() == currentItem) { // currentItem is the left child of parent parentItem.setPrevious(currentItem.previous()); } else { // parentItem == currentItem which is root root = currentItem.previous(); } } else if (currentItem.previous() == null) { // no left branch, make parent link to the right branch if (parentItem.next() == currentItem) { // currentItem is the right child of parent parentItem.setNext(currentItem.next()); } else if (parentItem.previous() == currentItem) { // currentItem is the left child of parent parentItem.setPrevious(currentItem.next()); } else { // parentItem == currentItem which is root root = currentItem.next(); } } else { // both left and right exist // from the right branch, find the smallest value ListItem current = currentItem.next(); ListItem leftMostParent = currentItem; while (current.previous()!=null) { leftMostParent = current; current = current.previous(); } // now current is the smallest node in the right branch // put it into current deleting node currentItem.setValue(current.getValue()); // delete smallest node if (leftMostParent == currentItem) { // no leftmost node, so current points to // the smallest node currentItem.setNext(current.next()); } else { // set the smallest node's parent to point to // the smallest node's right child leftMostParent.setPrevious(current.next()); } } }
@Override public void traverse(ListItem listItem) { // recursive method if (listItem != null) { traverse(listItem.previous()); System.out.println(listItem.getValue()); traverse(listItem.next()); } }