Inheritance, accessibility modifiers, and method overriding are fundamental concepts in object-oriented programming (OOP), including Java. Let’s explore each of these concepts:
Overview
- What is Inheritance?
- Accessibility
- Method Overriding
- Calling the Superclass’s Constructors
- Type Casting
- The instanceof Keyword
What Is Inheritance?
- Concept of OOP, using which we organize classes in hierarchy.
- Used to avoid redundancy and avoid duplication
- Child classes are created using “extends” keyword.
- Parent class is called as “Super Class” and child is called as a “Sub Class”.
- No multiple inheritance in java.
- Inheritance creates “Is a” relationship between classes
- Let’s see the code.
Accessibility
- Within a package, a sub class can access all non-private members of super class
- Outside package, only public and protected members could be accessed by a sub class.
- Time to code.
Method Overriding
- When a sub class changes the behavior of a method of its parent class, that is known as method overriding.
- Method signature needs to be same for calling it method overriding.
- Different from method overloading
- Use “final” to prevent method overriding.