Java - Inheritance
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.
• 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.
• 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.
• 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.
• 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.
• 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
• 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
• Why use inheritance in java • For Method Overriding • (so runtime polymorphism can be achieved). • For Code Reusability.
• 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.
• 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
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(); • }}
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(); • }}
Has-A inheritance • class Operation{ • int square(int n){ • return n*n; • } • } • • class Circle{ • Operation op;//aggregation • double pi=3.14; • • double area(int radius){ • op=new Operation(); • int rsquare=op.square(radius);//code reusability (i.e. delegates the method call). • return pi*rsquare; • } • • • • public static void main(String args[]){ • Circle c=new Circle(); • double result=c.area(5); • System.out.println(result); • } • }
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.
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).
• 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 • } • }
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. •
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(); • }}
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(); • }}
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(); • }}
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
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(); • } • }
• class Shape{ • void draw(){System.out.println("drawing...");} • } • class Rectangle extends Shape{ • void draw(){System.out.println("drawing rectangle...");} • } • class Circle extends Shape{ • void draw(){System.out.println("drawing circle...");} • } • class Triangle extends Shape{ • void draw(){System.out.println("drawing triangle...");} • } • class TestPolymorphism2{ • public static void main(String args[]){ • Shape s; • s=new Rectangle(); • s.draw(); • s=new Circle(); • s.draw(); • s=new Triangle(); • s.draw(); • } • }
Thanks

Java - Inheritance_multiple_inheritance.pptx

  • 1.
  • 2.
    What is Inheritance? Inheritanceis 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 ofInheritance • 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 INHERITANCEis 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 useinheritance in java • For Method Overriding • (so runtime polymorphism can be achieved). • For Code Reusability.
  • 10.
    • The syntaxof 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 : Singleinheritance • 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 : MultilevelInheritance • 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(); • }}
  • 14.
    Has-A inheritance • classOperation{ • int square(int n){ • return n*n; • } • } • • class Circle{ • Operation op;//aggregation • double pi=3.14; • • double area(int radius){ • op=new Operation(); • int rsquare=op.square(radius);//code reusability (i.e. delegates the method call). • return pi*rsquare; • } • • • • public static void main(String args[]){ • Circle c=new Circle(); • double result=c.area(5); • System.out.println(result); • } • }
  • 15.
    Method Overriding inJava • 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 JavaMethod 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 inJava • 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 usedto 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 beused 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 usedto 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 InJava • 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(); • } • }
  • 24.
    • class Shape{ •void draw(){System.out.println("drawing...");} • } • class Rectangle extends Shape{ • void draw(){System.out.println("drawing rectangle...");} • } • class Circle extends Shape{ • void draw(){System.out.println("drawing circle...");} • } • class Triangle extends Shape{ • void draw(){System.out.println("drawing triangle...");} • } • class TestPolymorphism2{ • public static void main(String args[]){ • Shape s; • s=new Rectangle(); • s.draw(); • s=new Circle(); • s.draw(); • s=new Triangle(); • s.draw(); • } • }
  • 25.