2. What is Inheritance?
Inheritance is a mechanism in which one
class acquires the property of another class.
For example, a child inherits the traits of
his/her parents.
With inheritance, we can reuse the fields
and methods of the existing class. Hence,
inheritance facilitates Reusability and is an
important concept of OOPs.
3. • Types of Inheritance
• There are Various types of inheritance :
• Single Inheritance:
• In Single Inheritance one class extends another
class (one class only).
•
Single Inheritance
• In above diagram, Class B extends only Class A.
Class A is a super class and Class B is a Sub-class.
4. • Multiple Inheritance:
• In Multiple Inheritance, one class extending more
than one class. Java does not support multiple
inheritance.
• Multiple Inheritance
• As per above diagram, Class C extends Class A
and Class B both.
5. • Multilevel Inheritance:
• In Multilevel Inheritance, one class can inherit
from a derived class. Hence, the derived class
becomes the base class for the new class.
• Multilevel Inheritance
• As per shown in diagram Class C is subclass of
B and B is a of subclass Class A.
6. • Hierarchical Inheritance:
• In Hierarchical Inheritance, one class is
inherited by many sub classes.
• Hierarchical Inheritance As per above
example, Class B, C, and D inherit the same
class A.
7. • Hybrid Inheritance:
• Hybrid inheritance is a combination of Single
and Multiple inheritance.
• As per above example, all the public and
protected members of Class A are inherited
into Class D, first via Class B and secondly via
Class C.
• Note: Java doesn't support hybrid/Multiple
inheritence
8. • JAVA INHERITANCE is a mechanism in which one
class acquires the property of another class.
• In Java, when an "Is-A" relationship exists
between two classes, we use Inheritance.
• super class
• Subclass
• Extends
9. • Why use inheritance in java
• For Method Overriding
• (so runtime polymorphism can be achieved).
• For Code Reusability.
10. • The syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
• The extends keyword indicates that you are
making a new class that derives from an existing
class. The meaning of "extends" is to increase the
functionality.
11. • class Employee{
• double salary=40000;
• }
• class Programmer extends Employee{
• int bonus=10000;
• }
•
• class Main{
• public static void main(String args[]){
• Programmer p=new Programmer();
• System.out.println("Programmer salary is:"+p.salary);
• System.out.println("Bonus of Programmer is:"+p.bonus);
• }
• }
• https://compiler.javatpoint.com/opr/online-java-compiler.jsp
12. IS-A relationship :
Single inheritance
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void bark(){System.out.println("barking...");}
• }
• class Main{
• public static void main(String args[]){
• Dog d=new Dog();
• d.bark();
• d.eat();
• }}
13. IS-A relationship :
Multilevel Inheritance
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void bark(){System.out.println("barking...");}
• }
• class BabyDog extends Dog{
• void weep(){System.out.println("weeping...");}
• }
• class Main{
• public static void main(String args[]){
• BabyDog d=new BabyDog();
• d.weep();
• d.bark();
• d.eat();
• }}
15. Method Overriding in Java
• If subclass (child class) has the same method
as declared in the parent class, it is known
as method overriding in Java.
16. Rules for Java Method Overriding
• The method must have the same name as in
the parent class
• The method must have the same parameter
as in the parent class.
• There must be an IS-A relationship
(inheritance).
17. • class Vehicle{
• //defining a method
• void run(){System.out.println("Vehicle is running");}
• }
• //Creating a child class
• class Bike2 extends Vehicle{
• //defining the same method as in the parent class
• void run(){System.out.println("Bike is running safely");}
•
• public static void main(String args[]){
• Bike2 obj = new Bike2();//creating object
• obj.run();//calling method
• }
• }
18. Super Keyword in Java
• The super keyword in Java is a reference variable which is used to
refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.
• Usage of Java super Keyword
• super can be used to refer immediate parent class instance variable.
• super can be used to invoke immediate parent class method.
• super() can be used to invoke immediate parent class constructor.
•
19. super is used to refer immediate
parent class instance variable.
• class Animal{
• String color="white";
• }
• class Dog extends Animal{
• String color="black";
• void printColor(){
• System.out.println(color);//prints color of Dog class
• System.out.println(super.color);//prints color of Animal class
• }
• }
• class TestSuper1{
• public static void main(String args[]){
• Dog d=new Dog();
• d.printColor();
• }}
20. super can be used to invoke parent
class method
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void eat(){System.out.println("eating bread...");}
• void bark(){System.out.println("barking...");}
• void work(){
• super.eat();
• bark();
• }
• }
• class TestSuper2{
• public static void main(String args[]){
• Dog d=new Dog();
• d.work();
• }}
21. super is used to invoke parent class
constructor
• class Animal{
• Animal(){System.out.println("animal is created");}
• }
• class Dog extends Animal{
• Dog(){
• super();
• System.out.println("dog is created");
• }
• }
• class TestSuper3{
• public static void main(String args[]){
• Dog d=new Dog();
• }}
22. Final Keyword In Java
• The final keyword in java is used to restrict
the user. The java final keyword can be used in
many context. Final can be:
• variable
• method
• class
23. Java Runtime Polymorphism
• class Bike{
• void run(){System.out.println("running");}
• }
• class Splendor extends Bike{
• void run(){System.out.println("running safely with 60km");
}
•
• public static void main(String args[]){
• Bike b = new Splendor();//upcasting
• b.run();
• }
• }