Inheritance
Inheritance
INHERITANCE
Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. A class that is inherited is called a superclass. The class that does the inheriting is called a subclass.
EXAMPLE
// A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height;
void showDim() {
System.out.println("Width and height are " + width + " and " + height); } }
// A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { String style; double area() {
t1.width = 4.0; t1.height = 4.0; t1.style = "isosceles"; t2.width = 8.0; t2.height = 12.0;
t2.style = "right";
System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println("Info for t2: "); t2.showStyle();
t2.showDim();
System.out.println("Area is " + t2.area()); } }
TYPES OF INHERITANCE
Single Inheritance
A
A B
Hierarchal Inheritance
A B D E C
Multiple Inheritance
A C A F C B
Hybrid Inheritance
B D E
void showStyle() {
System.out.println("Triangle is " + style); }
class Shapes3 { public static void main(String args[]) { Triangle t1 = new Triangle("isosceles", 4.0, 4.0); Triangle t2 = new Triangle("right", 8.0, 12.0); System.out.println("Info for t1: "); t1.showStyle();
t1.showDim();
System.out.println("Area is " + t1.area()); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); } }
SUPER KEYWORD
class TwoDShape { private double width; private double height; // Parameterized constructor.
TwoDShape(double w, double h) {
width = w; height = h; }
10
// Accessor methods for width and height. double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; }
void showDim() {
System.out.println("Width and height are " + width + " and " + height); } }
11
void showStyle() {
System.out.println("Triangle is " + style); } }
12
class Shapes4 { public static void main(String args[]) { Triangle t1 = new Triangle("isosceles", 4.0, 4.0); Triangle t2 = new Triangle("right", 8.0, 12.0);
13
EXAMPLE - 2
class A { int i; } // Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A B(int a, int b) { super.i = a; // i in A i = b; // i in B }
14
void show() { System.out.println("i in superclass: " + super.i); System.out.println("i in subclass: " + i);
}
} class UseSuper { public static void main(String args[]) { B subOb = new B(1, 2); subOb.show(); } }
15
}
// Create a subclass by extending class A. class B extends A { B() { System.out.println("Constructing B."); } }
16
}
class OrderOfConstruction { public static void main(String args[]) {
C c = new C();
} }
17
super(j);
b = i; } }
18
class SupSubRef {
public static void main(String args[]) { X x = new X(10); X x2; Y y = new Y(5, 6); x2 = x; // OK, both of same type System.out.println("x2.a: " + x2.a);
x2.a = 19; // OK
// x2.b = 27; Error, X doesn't have a b member } }
19
METHOD OVERRIDING
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.
20
21
class A { int i, j; A(int a, int b) { i = a; j = b; } // display i and j void show() { System.out.println("i and j: "+ i +" "+j); } } class B extends A { B(int a, int b, int c) { super(a, b); k = c; }
// display k this overrides show() in A void show() { System.out.println("k: " + k); } } class Override { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); // this calls show() in B } }
22
}
} class Sub1 extends Sup { void who() { System.out.println("who() in Sub1"); } }
23
class Sub2 extends Sup { void who() { System.out.println("who() in Sub2"); } } class DynDispDemo { public static void main(String args[]) { Sup superOb = new Sup(); Sub1 subOb1 = new Sub1(); Sub2 subOb2 = new Sub2(); Sup supRef; supRef = superOb; supRef.who(); supRef = subOb1; supRef.who(); supRef = subOb2; supRef.who(); } }
24
25
ABSTRACT METHODS
An abstract method is created by specifying the abstract type modifier. An abstract method contains no body and is, therefore, not implemented by the superclass. Thus, a subclass must override it The abstract modifier can be used only on normal methods It cannot be applied to static methods or to constructors. To declare an abstract method, use this general form:
abstract type name(parameter-list); Ex: abstract void moveTo(int deltaX, int deltaY);
26
ABSTRACT CLASS
Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. An abstract class cannot be directly instantiated, but can be inherited. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract. 27
abstract class A { abstract void callme(); // concrete methods are still allowed void callmetoo() { System.out.println("This is a concrete method."); } } class B extends A { void callme() { System.out.println("B's implementation of callme."); } } class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); } }
28
29
class A { final void meth() { System.out.println("This is a final method."); } } class B extends A { void meth() { // ERROR! Can't override. System.out.println("Illegal!"); } }
final class A { // ... } // The following class is illegal. class B extends A { // ERROR! Can't subclass A // ... }
30
OBJECT CLASS
All other classes are subclasses of Object. This means that a reference variable of type Object can refer to an object of any other class. Also, since arrays are implemented as classes, a variable of type Object can also refer to any array.
31
TYPE CASTING
byte int
If the two types are incompatible, then we need to perform the conversion manually.(i.e explicit conversion). This is known as casting. Example:
double long
33
AUTOMATIC CONVERSIONS
An automatic type conversion will take place if the following two conditions are met:
The two types are compatible. The destination type is larger than the source type.
This can be made by assigning. Example: int i; float f; i = 10; f = i; This automatic type conversion is known as widening conversion. There are no automatic conversions from the numeric types to char or boolean, char to/from boolean.
34
EXAMPLE
/* Demonstrate automatic conversion from long to double. */
class LtoD {
public static void main(String args[]) { long L; double D; L = 1001234; D = L; // Just assigning System.out.println("L and D: " + L + " " + D); } }
35
36
// Demonstrate casts. class Conversion { public static void main(String args[]) { byte b; int i = 257;
double d = 323.142;
System.out.println("Conversion of int to byte."); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("Conversion of double to int."); i = (int) d; System.out.println("d and i " + d + " " + i);
37
CLASS CASTING
Conversion of a reference of a class to another class is known as Class Casting. Converting to a base class reference is known as Up Casting. Converting to a sub class reference is known as Down Casting. Up casting can be automatic. But, down casting is done only by manually. General form: class-name ref = (target class-name)object
38
EXAMPLE
// Class casting (Upcasting and Downcasting) class A{
int x;
void add() { System.out.println(" X : "+x);
}
} class B extends A{ int y; void sum() { System.out.println(" Y } } : "+y);
39
class ClassCasting{ public static void main(String aa[]) { // o1 is Reference of B and its also object B B o1=new B();
o1.x=100;
o1.y=200; o1.add(); o1.sum(); System.out.println("----------------------"); /* o2 is Reference of A and its a object B Upcasting */
A o2=(A)o1;
o2.add(); System.out.println("----------------------");
40
/* o3 is Ref of Object and its a obj B -Automatic Upcasting */ Object o3 = o2; System.out.println("----------------------"); //Downcasting A o4=(A)o3; o4.add(); System.out.println("----------------------"); //Downcasting B o5=(B)o4;
o5.add();
o5.sum(); }
41