Inheritance
- It is the process by which object of one class can acquire the properties of object of another class.
- The class, from which properties are inherited, is known as super class.
- The class, to which properties are inherited, is known as sub class. A sub class inherits all of the variables and methods defined by the super class and add its own, unique elements.
- Inheritance allows the creation of hierarchical classifications.
- To derive a sub class from super class, we use the extends keyword.
- The general form of a class declaration that inherits a superclass is :
1 2 3 4 |
class subclass-name extends superclass-name { // body of class } |
A sub class can access all the public and protected members of a super class. (By default, the variable or function of a class are public in nature)
Demonstration of Simple Inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class A { int i, j; void showij() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; void showk() { System.out.println("k:" + k); } void sum() { System.out.println (i+j+k); } } class SimpleInheritance { public static void main(String args[]) { A sup = new A ();THE JAVA LANGUAGE B sub = new B (); sup.i = 10; sup.j = 20; System.out.println("Contents of super class "); sup.showij(); sub.i = 7; sub.j = 8; sub.k = 9; System.out.println("Contents of sub class"); sub.showij(); sub.showk(); System.out.println("Sum of i, j and k in sub class"); sub.sum(); } } |
A sub class cannot access private members of a super class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class A { int i; // public by default private int j; // private to A void setij(int x, int y) { i = x; j = y; } } class B extends A { int total; void sum() { total = i + j; // ERROR, j is not accessible as it is private to A } } class Access { public static void main(String args[]) { B sub= new B(); sub.setij(10, 12); sub.sum(); System.out.println("Total is " + sub.total); } } |
Remember
A class member that has been declared as private will remain private to its class. It is not accessible by any code outside its class, including subclasses. (i.e. a subclass cannot access the private or protected member of a super class.)
Demonstration of Inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
class Box { double width, height, depth; Box(Box ob) { width = ob.width; height = ob.height; depth = ob.depth; } Box(double w, double h, double d) { width = w; height = h; depth = d; } Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } Box(double len) { width = height = depth = len; } double volume() { return (width * height * depth); } } class BoxWeight extends Box { double weight; BoxWeight(double w, double h, double d, double m) { width = w; height = h; depth = d; weight = m; } } { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight); } } class DemoBoxWeight |
How Constructor method of a Super class gets called
A subclass constructor invokes the constructor of the super class implicitly.When a BoxWeight object, a subclass, is instantiated, the default constructor of its super class, Box class, is invoked implicitly before sub-class’s constructor method is invoked.
A subclass constructor can invoke the constructor of the super explicitly by using the “super” keyword.The constructor of the BoxWeight class can explicitly invoke the constructor of the Box class using “super” keyword.
In a class hierarchy, constructors are called in order of derivation, from super class to subclass.
Since super( ) must be the first statement executed in a subclass’ constructor, this order is the same whether or not super( ) is used.
If super( ) is not used, then the default of each super class will be executed.
Demonstration of subclass constructor invoking the constructor of the super class implicitly.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class A { A() { System.out.println("Inside A's constructor."); } } class B extends A { B() { System.out.println("Inside B's constructor."); } } class C extends B { C() { System.out.println("Inside C's constructor."); } } class CallingCons { public static void main(String args[]) { C c = new C(); } } |
Output:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
Demonstration of Subclass constructor calling super class’s constructor using the keyword super.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Rect { int length, breadth; Rect (int l, int b) { length=l; breadth=b; } } class Cuboid extends Rect { double height; Cuboid (int l, int b, int h) { super(l, b); height=h; } void volume() { System.out.println (length*breadth*height); } }THE JAVA LANGUAGE class Vol { public static void main(String args[]) { Cuboid v=new Cuboid(1, 2, 3); v.volume(); } } |
1 |
Output: 6 |
Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its super class, then the method in the subclass is said to override the method in the super class.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden.
Demonstration of Method Overriding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show() { System.out.println("k: " + k); } } class Override { public static void main(String args[]) { B sub= new B(1, 2, 3); sub.show(); // this calls show() in B } } |
When show ( ) is invoked on an object of type B, the version of show ( ) defined within B is used. That is,
the version of show ( ) inside B overrides the version declared in A.If you wish to access the super class version of an overridden function, you can do so by using super
Dynamic Method Dispatch
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
Demonstration of Dynamic Method Dispatch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class A { void callme() { System.out.println("Inside A's callme method"); } } class B extends A { void callme() { System.out.println("Inside B's callme method"); } } class C extends A void callme() { System.out.println("Inside C's callme method"); } } class Dispatch { <em>public static void main(String args[])</em> <em>{</em> <em>A a = new A();</em> <em>B b = new B();</em> <em>C c = new C();</em> <em>A r; // obtain a reference of type A</em> <em>r = a; // r refers to an A object</em> <em>r.callme(); // calls A's version of callme</em> <em>r = b; // r refers to a B object</em> <em>r.callme(); // calls B's version of callme</em> <em>THE JAVA LANGUAGE</em> <em>r = c; // r refers to a C object</em> <em>r.callme(); // calls C's version of callme</em> <em>}</em> <em>}</em> |
1 2 3 4 |
Output: Inside A’s callme method Inside B’s callme method Inside C’s callme method |
Abstract class
Abstract classes are the class which contains method without providing a complete implementation (or
without having background details). It is not possible create object of an abstract class. Abstract classes
can include methods having all details.
Demonstration of abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
abstract class A { abstract void callme(); // concrete methods are still allowed in abstract classes 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(); } } |
Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java’s approach to run-time polymorphism is implemented through the use of super class references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object.
Uses of final Keyword: To Prevent Inheritance
The keyword final has three uses:
1. First, it can be used to create constant variable whose value cannot be changed.
2. To Prevent method Overriding
• While method overriding is one of Java’s most powerful features, there will be times when you will want to prevent it from occurring.
• To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class A { final void meth() { System.out.println("This is a final method."); } } class B extends A { void meth() { System.out.println("Illegal!"); } } |
Because meth ( ) is declared as final, it cannot be overridden in B. If you attempt to do so, a compiletime error will result.
3. To Prevent Inheritance
Sometimes we want to prevent a class from being inherited. To do this, precede the class declaration with the keyword final. Declaring a class as final implicitly declares all of its methods as final, too.
1 2 3 4 5 6 7 8 9 10 |
final class A { // ... } // The following class is illegal. class B extends A { // ERROR! Can't subclass A // ... } |
As the comments imply, it is illegal for B to inherit A since A is declared as final.
The Object Class
There is one special class, Object, defined by Java. All other classes are subclasses of Object. That is, Object is a super class of all other classes. This means that a reference variable of type Object can refer to an object of any other class.
Questions
Short Type Questions
1. Define inheritance. What are sub class and super class?
2. Can a top level class be made private or protected? Give reason for your answer.
3. Write any two use of final keyword.
4. What is dynamic method dispatch?
5. Java does not provide multiple inheritances. Give reason. How multiple inheritances can be achieved in java?
6. Define Object class.
7. Differentiate between early binding and late binding.
8. How can you call the constructor of a super class explicitly? Illustrate with an example.
9. Write any two use of super keyword.
10. Define abstract class.
Long Type Questions
1. Define inheritance. Multiple inheritances in java cannot be achieved as it leads to ambiguity problem. Justify your answer with suitable example.
2. What is method overriding? How it can prevented? Explain with example.
3. Classes declared as final cannot be inherited (T or F)? Justify your answer with suitable example.
4. How calls to overridden methods are resolved during program execution? Explain.