2. What is Inheritance?
Inheritance in java is a mechanism in which one object
acquires all the properties and behaviors of parent object.
Inheritance represents the IS-A relationship, also known
as parent-child relationship.
4. Java Supports a special feature called interface. This feature
helps to connect a class with more than one classes.
For this type of connectivity java uses ‘implements’ keyword.
Syntax :
interface A{
……}
Interface B {
____}
class M {
-------}
class N implements A,B extends M{
=====
_____------…………}
Indirect Mechanism of Inheritance
5. Sample Code of Inheritance
class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
6. Advantages of Inheritance
● Reusability -- facility to use public methods of base class
without rewriting the same
● Extensibility -- extending the base class logic as per business
logic of the derived class
● Data hiding -- base class can decide to keep some data private so
that it cannot be altered by the derived class
● Overriding -- With inheritance, we will be able to override the
methods of the base class so that meaningful implementation of
the base class method can be designed in the derived class.
7. Disadvantages of Inheritance
• Unsupported- Java not support Multiple inheritance as well as
Hybrid inheritance.
• Permits only one class- The extends keyword permits to connect
a class with only one class.
• Undefined- In Interface, properties are only declared and
assigned, but never defined.
• Re-factor- If a method is deleted in the "super class" or
aggregate, then we will have to re-factor in case of using
that method.