JAVA
Inheritanc Manish Tiwari
Created by
Presentation By
Manish Tiwari,
Assistant Professor,
Software Engineer,
Computer Science and Application
Find Notes and Material
2. WhatsApp: ?
4. Blog: manishtiwarise.blogspot.in
3. Email: tiwarikmanish@gmail.com
1. Twitter: @manishtiwarise
5. Slide Share:
https://www.slideshare.net/tiwarikmanish
The class which inherits the properties of other is known as subclass
(derived class, child class) and the class whose properties are inherited
is known as superclass (base class, parent class).
Inheritance can be defined as the process where one class acquires
the properties (methods and fields) of another. With the use of
inheritance the information is made manageable in a hierarchical order.
1. Inheritance allows a us to derive a new class from an existing
one.
2. The existing class is called the parent class, or superclass, or
base class.
3. The derived class is called the child class or subclass.
4. As the name implies, the child inherits characteristics of the
parent.
5. That is, the child class inherits the methods and data defined for
the parent class.
Definition
Subclass and Superclass
Notes
Real life example of inheritance
The real life example of inheritance is child and parents, all the properties of
father are inherited by his son.
extends is the keyword used to inherit the properties of a
java class. Following is the syntax of extends keyword.
Extends Keyword
Syntax
class Super
{ .....
.....
}
class Sub extends Super
{ .....
.....
}
Types of Inheritance
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.
Therefore following is illegal.
Example-1:
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(""+z);
}
}
Contd..
public class MyCalculation extends Calculation
{
public void multiplication(int x, int y)
{
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
}
class exe_calc
{
public static void main(String args[])
{
int a = 20, b = 10;
MyCalculation demo = new MyCalculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
One of the key benefits of inheritance is to minimize the amount of duplicate
code in an application by sharing common code amongst several subclasses.
Where equivalent code exists in two related classes, the hierarchy can usually
be refactored to move the common code up to a mutual superclass. This also
tends to result in a better organization of code and smaller, simpler
compilation units. Inheritance can also make application code more flexible to
change because classes that inherit from a common superclass can be used
interchangeably. If the return type of a method is superclass
1. Minimize the amount of duplicate code in an application:
If duplicate code (variable and methods) exists in two related
classes, we can refactored that hierarchy by moving that
common code up to the common superclass.
2. Better organization of code: Moving of common code to
superclass results in better organization of code.
3. Code more flexible change: Inheritance can also make
application code more flexible to change because classes that
inherit from a common superclass can be used
interchangeably. If the return type of a method is superclass.
Advantage of Inheritance
Disadvantages
1.One of the main disadvantages of inheritance in Java (the
same in other object-oriented languages) is the increased
time/effort it takes the program to jump through all the levels of
overloaded classes. If a given class has ten levels of
abstraction above it, then it will essentially take ten jumps to
run through a function defined in each of those classes
2.Main disadvantage of using inheritance is that the two
classes (base and inherited class) get tightly coupled. This
means one cannot be used independent of each other.
3. Also with time, during maintenance adding new features
both base as well as derived classes are required to be
changed. If a method signature is changed then we will be
affected in both cases (inheritance & composition)
Thank
You

Java Inheritance

  • 1.
  • 2.
    Presentation By Manish Tiwari, AssistantProfessor, Software Engineer, Computer Science and Application
  • 3.
    Find Notes andMaterial 2. WhatsApp: ? 4. Blog: manishtiwarise.blogspot.in 3. Email: tiwarikmanish@gmail.com 1. Twitter: @manishtiwarise 5. Slide Share: https://www.slideshare.net/tiwarikmanish
  • 4.
    The class whichinherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. 1. Inheritance allows a us to derive a new class from an existing one. 2. The existing class is called the parent class, or superclass, or base class. 3. The derived class is called the child class or subclass. 4. As the name implies, the child inherits characteristics of the parent. 5. That is, the child class inherits the methods and data defined for the parent class. Definition Subclass and Superclass Notes
  • 5.
    Real life exampleof inheritance The real life example of inheritance is child and parents, all the properties of father are inherited by his son.
  • 6.
    extends is thekeyword used to inherit the properties of a java class. Following is the syntax of extends keyword. Extends Keyword Syntax class Super { ..... ..... } class Sub extends Super { ..... ..... }
  • 7.
    Types of Inheritance Avery important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore following is illegal.
  • 8.
    Example-1: class Calculation { int z; publicvoid 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(""+z); } } Contd..
  • 9.
    public class MyCalculationextends Calculation { public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given numbers:"+z); } } class exe_calc { public static void main(String args[]) { int a = 20, b = 10; MyCalculation demo = new MyCalculation(); demo.addition(a, b); demo.Subtraction(a, b); demo.multiplication(a, b); } }
  • 10.
    One of thekey benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refactored to move the common code up to a mutual superclass. This also tends to result in a better organization of code and smaller, simpler compilation units. Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass 1. Minimize the amount of duplicate code in an application: If duplicate code (variable and methods) exists in two related classes, we can refactored that hierarchy by moving that common code up to the common superclass. 2. Better organization of code: Moving of common code to superclass results in better organization of code. 3. Code more flexible change: Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass. Advantage of Inheritance
  • 11.
    Disadvantages 1.One of themain disadvantages of inheritance in Java (the same in other object-oriented languages) is the increased time/effort it takes the program to jump through all the levels of overloaded classes. If a given class has ten levels of abstraction above it, then it will essentially take ten jumps to run through a function defined in each of those classes 2.Main disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled. This means one cannot be used independent of each other. 3. Also with time, during maintenance adding new features both base as well as derived classes are required to be changed. If a method signature is changed then we will be affected in both cases (inheritance & composition)
  • 12.