Inheritance in Java
What is Inheritance
In Java, a process involving the collection of all the parent object's properties and actions by the child object is known as Inheritance.
The Inheritance cycle is one of the main features of OOP (Object-oriented Programming) through which one class obtains the properties and functionalities of another class.
- Class: A class is a set of objects that have common properties. It is a model or a blueprint that creates objects.
- Super Class: Within this class, whose inherited characteristics are known as superclass (or parent or foundation class).
- Subclass: The class inheriting the other class is considered a subclass (or an expanded, or infant class derivative). The subclass must incorporate its own fields and methods, in addition to the superclass fields and methods.
Types of Inheritance in java
- Single
- Multilevel
- Hierarchical
Single inheritance is simply the inheritance, where it happens within class only.
Multilevel inheritance In Java language, It is essentially the process when a single class inherits the properties and methods of the multiple classes of different levels.
Hierarchical Inheritance In Java language, It is essentially the situation where one class acts as a superclass with more than one subclass.
Java Inheritance Example
Here is an example of the Java inheritance that will allow you to understand its physical side.
class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; 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); } }
Comments
Post a Comment