Java Inheritance and Polymorphism
Can you inherit Constructor of the superclass?
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Key point of polymorphism is
A subclass is a specialization of its superclass every instance of a subclass is also an instance of its superclass, but not vice versa. Every father is man. But not all men are fathers.
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.
Can you extend more than one class?
A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class.
Give me example of Polymorphism that you use in your code?
Can use Polymorphism to catch exceptions by their superclass. Do not do it in your code. This is a baring exception and people will hate you very fast. so no catch (Exception e)
What is use interface in Java?
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.
IS-A Relationship
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
Inheritance is
Inheritance, which is the act of having one class adopt the properties and methods of another class. This prevents code duplication and allows you to share code across classes while having the source code live in only one class file.
Invoking Superclass Constructor
Invoking Superclass Constructor If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. super(values);
The instanceof Keyword
Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal.
Polymorphism is
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Real life example of polymorphism
Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.
The super keyword
The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used. It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.
Why use Java interface?
There are mainly three reasons to use interface. They are given below. It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling.
HAS-A relationship
These relationships are mainly based on the usage. This determines whether a certain class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.