1 / 17

Inheritance and interfaces

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

temple
Download Presentation

Inheritance and interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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; }

  3. 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()

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. GeoObject • public abstract class GeoObject{ • private String color=“white”; • public String getColor(){return color;} • … • public abstract double findArea(); • public abstract double findPerimeter(); • }

  9. 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; • } • }

  10. 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

  11. 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()); • }

  12. 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

  13. 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”);

  14. 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);

  15. 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();

  16. Sorting an array of objects • Write a static method for sorting an array of comparable objects • They are compared using compareTo method

  17. 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”);

More Related