Object Oriented Programming Andi Nurkholis, S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 April 16, 2020
7.1 Inheritance 2
Inheritance in Java Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features (fields and methods) of another class. 3
4 Important Terminology • Super Class: The class whose features are inherited is known as super class(or a base class or a parent class). • Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
5 Important Terminology (cont.) • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
How to Use Inheritance in Java? The keyword used for inheritance is extends. Syntax: 6 class derived-class extends base-class { //methods and fields }
Inheritance Illustration 7
Illustration Explanation In previous illustration, when an object of MountainBike class is created, a copy of the all methods and fields of the superclass acquire memory in this object. That is why, by using the object of the subclass we can also access the members of a superclass. Please note that during inheritance only object of subclass is created, not the superclass. For more, refer Java Object Creation of Inherited Class. 8
9 Types of Inheritance There are Various types of inheritance: • Single Inheritance • Multiple Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Hybrid Inheritance Note: Java doesn't support hybrid/Multiple inheritance
Single Inheritance In Single Inheritance one class extends another class (one class only). 10 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. 11 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. As per shown in diagram Class C is subclass of B and B is a of subclass Class A. 12
13 Hierarchical Inheritance In Hierarchical Inheritance, one class is inherited by many sub classes. 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. Java doesn't support hybrid inheritance 14 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.
Important Facts of Inheritance in Java • Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class. • Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java. 15
Important Facts of Inheritance in Java (cont.) • Inheriting Constructors: 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. • Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass. 16
So, Why and When to Use "Inheritance"? The most important use is the reusability of code. The code that is present in the parent class doesn’t need to be written again in the child class. What is the meaning? With inheritance, we can reuse attributes and methods of an existing class when you create a new class. 17
Thank You, Next … Polymorphism Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. April 16, 2020

Object Oriented Programming - 7.1. Inheritance

  • 1.
    Object Oriented Programming Andi Nurkholis,S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 April 16, 2020
  • 2.
  • 3.
    Inheritance in Java Inheritanceis an important pillar of OOP (Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features (fields and methods) of another class. 3
  • 4.
    4 Important Terminology • SuperClass: The class whose features are inherited is known as super class(or a base class or a parent class). • Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • 5.
    5 Important Terminology (cont.) •Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
  • 6.
    How to UseInheritance in Java? The keyword used for inheritance is extends. Syntax: 6 class derived-class extends base-class { //methods and fields }
  • 7.
  • 8.
    Illustration Explanation In previousillustration, when an object of MountainBike class is created, a copy of the all methods and fields of the superclass acquire memory in this object. That is why, by using the object of the subclass we can also access the members of a superclass. Please note that during inheritance only object of subclass is created, not the superclass. For more, refer Java Object Creation of Inherited Class. 8
  • 9.
    9 Types of Inheritance Thereare Various types of inheritance: • Single Inheritance • Multiple Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Hybrid Inheritance Note: Java doesn't support hybrid/Multiple inheritance
  • 10.
    Single Inheritance In SingleInheritance one class extends another class (one class only). 10 In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.
  • 11.
    Multiple Inheritance In MultipleInheritance, one class extending more than one class. Java does not support multiple inheritance. 11 As per above diagram, Class C extends Class A and Class B both.
  • 12.
    Multilevel Inheritance In MultilevelInheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class. As per shown in diagram Class C is subclass of B and B is a of subclass Class A. 12
  • 13.
    13 Hierarchical Inheritance In HierarchicalInheritance, one class is inherited by many sub classes. As per above example, Class B, C, and D inherit the same class A.
  • 14.
    Hybrid Inheritance Hybrid inheritanceis a combination of Single and Multiple inheritance. Java doesn't support hybrid inheritance 14 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.
  • 15.
    Important Facts ofInheritance in Java • Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class. • Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java. 15
  • 16.
    Important Facts ofInheritance in Java (cont.) • Inheriting Constructors: 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. • Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass. 16
  • 17.
    So, Why andWhen to Use "Inheritance"? The most important use is the reusability of code. The code that is present in the parent class doesn’t need to be written again in the child class. What is the meaning? With inheritance, we can reuse attributes and methods of an existing class when you create a new class. 17
  • 18.
    Thank You, Next… Polymorphism Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. April 16, 2020