Inheritance in Java
Inheritance is a key principle of object-oriented programming. It entails the transfer of the existing structure of one class, including its constructor, variables, and methods, to a different class. The new class is called the child class (or subclass), while the one it's inheriting from is called the parent class (or superclass). We say that the child class extends the parent one. The child class is said to extend the parent class in the sense that it not only inherits whatever structures are defined by the parent, but it also creates new structures. The following example shows a parent class and how the child class extends it by adding a new method to it. We will take the Computer
class we defined earlier as a parent and create a new class called Tablet
, which is a type of computer.
Example05.java
20Â class Tablet extends Computer { 21Â Â Â Â Â // variables 22Â Â Â Â Â private double screenSize;Â Â ...