Unit 8 : Inheritance

3rd Semester

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 :

 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.









A sub class cannot access private members of a super class.

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.





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:

 
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.

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.

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.

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.





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.

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.

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.