Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn

Simplilearn
3 Mar 202014:01

Summary

TLDRThe video tutorial explains inheritance in Java using multiple examples and classes. It starts with basic concepts of superclasses and subclasses to demonstrate single inheritance. Then it covers multi-level inheritance by extending classes in a chain and hierarchical inheritance with one superclass and multiple subclasses. Real-world mobile company examples are used to show how classes inherit properties and methods from their parent classes. The examples employ getters, setters and constructors to manipulate inherited variables and output details. The summary concludes that various inheritance mechanisms allow code reuse and reduce duplication in Java OOP.

Takeaways

  • 😀 Inheritance is when one class inherits properties and attributes from another class
  • 👨‍💻 The types of inheritance in Java: single, multi-level, and hierarchical
  • 🔀 In single inheritance, one class inherits from one other class
  • ⏩ In multi-level inheritance, features trickle down from parent to child to grandchild classes
  • 🌳 Hierarchical inheritance is when multiple child classes inherit from one parent class
  • 📝 Use the 'extends' keyword to establish an inheritance link between classes
  • 🎯 Inheritance promotes code reuse by allowing derived classes to reuse base class code
  • ⚙️ The subclass can override or extend the functionality of the superclass
  • 😎 Getter and setter methods allow controlled access to class variables
  • 📈 Inheritance is useful in large programs when you need to manipulate many classes

Q & A

  • What is inheritance in Java?

    -Inheritance in Java is a mechanism where one class acquires the properties and behaviors of another class. The class that inherits properties is called subclass and the class whose properties are inherited is called superclass.

  • What are the different types of inheritance supported by Java?

    -The three main types of inheritance supported by Java are: 1) Single inheritance - one class inherits from one superclass 2) Multi-level inheritance - inheritance over multiple levels 3) Hierarchical inheritance - multiple subclasses inheriting from one superclass

  • How does single level inheritance work in Java?

    -In single level inheritance, one class inherits directly from one superclass. For example, class B inherits from class A. So class B can access all public and protected variables and methods of class A.

  • What is multi-level inheritance in Java?

    -In multi-level inheritance, a derived class inherits from a base class and that derived class also acts as a base class to another derived class. For example, Class C inherits from Class B, and Class B inherits from Class A. So Class C can access variables and methods of both Class A and B.

  • Explain hierarchical inheritance in Java.

    -In hierarchical inheritance, one base class has multiple subclasses inheriting from it. For example, Class A is a base class and Class B, Class C, Class D are subclasses derived from Class A. So Class B, C and D inherit the properties and behaviors of Class A.

  • What is the use of 'extends' keyword in Java?

    -The extends keyword is used by a subclass to inherit properties and behaviors from a superclass in Java. When a class extends another class using extends keyword, it can access all the non-private variables and methods of the superclass.

  • How can a subclass access the variables and methods of a superclass in Java?

    -A subclass can directly access the public and protected variables and methods of a superclass. For private variables, the subclass needs to use getter and setter methods defined in the superclass to access them.

  • Why is inheritance used in Java?

    -Inheritance is used in Java for code reuse and to establish is-a relationship between classes. It helps to reduce duplication of code as common properties and behaviors can be defined in a superclass and shared with subclasses.

  • What cannot be inherited in Java?

    -In Java, constructors and private members of a superclass cannot be inherited by subclasses. The subclasses can access private members only through methods of the superclass.

  • Can a class extend multiple classes in Java?

    -No, Java does not support multiple inheritance of classes. A class can extend only one parent class using extends keyword. However, it can implement multiple interfaces.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now