As an object oriented language Java obviously provides suppo

As an object oriented language, Java obviously provides support for composition and inheritance. Let’s start off by describing how Java specifies that one class inherits from another class. Are there any restrictions on inheritance in Java that distinguish it from C++? How does a derived class override a base class method? How does an overridden method invoke a base class method? How does the derived class constructor invoke an explicit value constructor from the base class?

Solution

Java Inheritance:

                             In Java inheritance play master role in inheriting ie. taking the property of another class to base class, which helps the coder in so many ways.

                            Here the class which inherits the property of other class is known as the sub class (also known as child class), and the class of one\'s property has been inherited is called as super class (also known as base class), extends is the keyword to inherit the class.

\" Inheritence itself a relationship - Parent-child relationship\". The following are the type inheritance available in Java.

                     1. Single level inheritance

                     2. Multi level inheritance

                     3. Hierarchial inheritance.

any restrictions on inheritance in Java that distinguish it from C++?

Yes, multiple inheritance is not supported in Java, because consider a situation, A,B,C are three classes. If C inherits A, B classes. If both A & B classes have same method and you call it from child class object, there will ambiguity to call method of A or B class.

To reduce complexity and simplification of concepts for the user, java doesn\'t allows multiple inheritance.

How does a derived class override a base class method?

You can access base class member in a sub class, even when those base members are overridden in the superclass using base keyword. You can create a derive class which contains with methods with same signature as in the parent class.

How does an overridden method invoke a base class method?

                                      You can make use of super keyword to override method invoke a base class.

Incase if you are using methods for initialization in code, you can make use of the constructor class and pass super keyword\"(super())\" inside the constructor of the other class.

Howerver, in certain situation if you want to call a method of super class, which is from the sub class method then you can use super keyword inside the subclass like \" :super.myMethod()\".

How does the derived class constructor invoke an explicit value constructor from the base class?

                                                It can be achieved through this() keyword or super() keyword. this() keyword is used to call another constructor of the same class.

As an object oriented language, Java obviously provides support for composition and inheritance. Let’s start off by describing how Java specifies that one class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site