February 2 Interface Cloning Inner Classes Interface z
February 2 • Interface • Cloning • Inner Classes
Interface z Java’s way of achieving multiple inheritance. z An interface is a skeleton of a class having method definition and constant data fields only. public interface Comparable { public int compare. To(Object o); } z Interface looks like a abstract class. z With an interface we define a type. z By implementing an interface, a class has another type (that of interface)
A Class may have multiple types Extends implements Employee Creative Manager Programmer Comparable Runnable Executive Programmer has three different types!!! Executive class has five different types!!!
public class Recruiter { public void hire(Creative candidate){ // put candidate into training list. } } public class Promoter { public void promote(Employee candidate){ // put candidate into higher position. } } public class Company { public static void main(String args[]) { Programmer hoony = new Programmer(); Recruiter peeker = new Recruiter(); Promoter booster = new Promoter(); peeker. hire(hoony); // hoony is Creative type!!! booster. promote(hoony); // hoony is Employee type!!! }
Interface versus Abstract Classes z Interface is just a specification of behavior Abstract class is an incomplete class which requires further implementation. z A class can implements several interfaces at once, but it can extends only one super class. z However, we need to implement methods to use interface or abstract classes. z Use an abstract class when you want to design a hierarchy of specialized class. z Use an interface when you want to access class with only defined methods.
Cloneable interface z When you make a copy of a object variable, you only copy the object handle(pointer). Car mustang = new Car(); Car civic = mustang; mustang civic They are pointing at the same object!!!
Cloneable cont. z To make a real copy of an object, we use clone() method defined in Object class. z However, clone() in Object is defined as protected. (Can not be directly called) z To use clone() inherited from Object, you must implement Cloneable interface. z You don’t have to override clone() method if you just want to use that of Object. Simply class A extends B implements Cloneable { } z Notice default clone() may not meet your need.
Defalut clone() is not for true cloning mustang engine door engine wheel door civic engine door wheel
Inner Class z Inner class is a class inside another class. Quite confusing!!! However, quite useful!!! z Recall that with has-a approach, a class only has references(handles) of objects of other classes. z With Is-a approach, a new class is created by being added on top of super class. z Inner class is defined within another class, no where else!!!
// Parcel class continues public void ship(String dest) { public class Parcel { Contents c = new Contents(2); private int weight; Destination d = class Contents { new Destination(dest); Contents(int item) { public static void main(String args[]) weight = item*30; { } // End of Contents Parcel parcel = new Parcel(); parcel. ship(“Tanzania”); class Destination{ } private String label; } // End of Parcel Destination(String where. To) { private String label; } String read. Label() { return label; } }// End of Destination
Inner Class cont. z If Class A has an Inner Class B, A is totally visible to B. But A is not. z Class A should create an instance of Class B to access into it. z Inner Class will be covered in much more concrete manner later. (Event handling) z Anonymous Inner Class is an Inner class without name and definition. This will also be covered thoroughly later.
- Slides: 11