170 likes | 366 Views
Inheritance and interfaces. A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass – child, derived, extended class
E N D
Inheritance and interfaces • A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass • Superclass-parent, base class • Subclass – child, derived, extended class • A subclass inherits functionality from its supperclass, and also creates new data and new methods. Subclasses usually have more functionality than their superclasses
Circle and cylinder public class Cylinder extends Circle{ private double length; public Cylinder(){ super(); // calls circle default constructor length = 1.0;} public Cylinder( double r, double m) { super(r); // calls another circle constructor length = m; } public double getLength(){ return length;} public double findVolume{ return findArea()*length; }
The Object class • Every class in Java is descended from Java.lang.Object class. • If no inheritance is specified when a class is defined. The supper class of the class is Object • Classes like Circle, String, StringBuffer are implicitly the child classes of Object • Three instance methods in object class are: • public boolean equals(Object obj) • public String toString() • public Object clone()
The clone method • Sometimes you need to make a copy of an object. Mistakenly, you might use the assignment operator • newObject = someobject; • This statement does not create a duplicate object. It simply assigns the reference of someobject to newobject. To create a new object with separate memory space, you need to use clone() method: • newObject = someObject.clone(); • This statement copies someobject to a new memory location and assigns the reference of the new object to newObject.
Protected, private and public • The protected data or methods in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in different packages.
final • Used on variable – constant • final double PI = 3.1415; • Used on methods – the methods cannot be modified by subclass • Used on class – the class can not be parent class, cannot be extended
Abstract classes • The class cannot have specific instances • Can have data and methods like regular classes • Can have abstract method • An abstract method is a method signature without implementation. The abstract methods are implemented in subclasses
GeoObject • public abstract class GeoObject{ • private String color=“white”; • public String getColor(){return color;} • … • public abstract double findArea(); • public abstract double findPerimeter(); • }
Redefine circle inherit from GeoObject • public class circle extends GeoObject{ • private double radius; • … • public double findAera(){ • return radius*radius*Math.PI; • } • public String toString(){ • return “[Circle] radius = “ + radius; • } • }
Casting objects and the instanceof operator • Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. • GeoObject ob = new Circle(); is known as implicit casting • But Circle cir = ob; will have compiler error • Use explicit casting • Circle cir = (Circle)ob; • It is always possible to cast an instance of a subclass to a variable of a supper class
When casting an instance of a superclass to a variable of its subclass, explicit casting must be used • You must make sure that the object to be cast is an instance of the subclass. • If not, a run time exception occurs. • // suppose myObj is declared as Object type • if( myObj instanceof Circle) • { Circle cir = (Circle)myObj; • System.out.println(“The circle area is” +cir.findArea()); • }
Processing primitive type values as objects • Primitive data types are not objects • Many methods require the use of object • Java offers corresponding class, called wrapper class • Integer, Double, Float, Long, Short, Byte, Character and Boolean
Numeric wrapper class • Constructors • Public Integer(int value) • Public Integer(String s) • Public Double(double value) • Public Double(String s) • Example: • Double Obj = new Double(5.0); • Double obj = new Double(“5.0”);
Numeric wrapper class constant • MAX_VALUE: represent the maximum value • MIN_VALUE: represent the minimum value • System.out.println(“maximum integer is” + Integer.MAX_VALUE);
Conversion methods • Each numeric wrapper class implements the abstract methods doubleValue, floatValue, intValue, longValue and shortValue, which are defined in the number class • Each wrapper class also overrides the toString and equals methods • Integer obj = new Integer(“23”); • int I = obj.intValue();
Sorting an array of objects • Write a static method for sorting an array of comparable objects • They are compared using compareTo method
Static valueOf methods • Wrapper class have a useful method • valueOf(string s) • This method creates a new object initialized to the value represented by the specified string • Double obj = Double.valueOf(“12.4”); • Integer obj = Integer.valueOf(“12”);