inheritance is a relationship in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history
Example of Child class referring Parent class
Example of Child class referring Parent class
Example of Child class calling Parent class
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history
Inheritance (IS-A)
Inheritance is one of the key features of Object Oriented Programming. Inheritance provided a mechanism that allowed a class to inherit the property of another class. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Superclass(Parent) and Subclass(child) in Java language.
Inheritance defines an is-a relationship between a Superclass and its Subclass.
extends
and keywordsimplements
are used to describe inheritance in Java.
Let us see how to extend keyword is used to achieve Inheritance.
class Vehicle. { ...... } class Car extends Vehicle { ....... //extends the property of vehicle class. }
Now based on the above example. In OOPs term we can say that,
- The vehicle is a superclass of Car.
- The car is a subclass of Vehicle.
- Car IS-A Vehicle.
Purpose of Inheritance
- To promote code reuse.
- To use Polymorphism.
The simple example of Inheritance
class Parent { public void p1() { System.out.println("Parent method"); } }
public class Child extends Parent { public void c1() { System.out.println("Child method"); } public static void main(String[] args) { Child cobj = new Child(); cobj.c1(); //method of Child class cobj.p1(); //method of Parent class } }Output :Child method Parent method
Another example of Inheritance
class Vehicle { String vehicleType; } public class Car extends Vehicle { String modelType; public void showDetail() { vehicleType = "Car"; //accessing Vehicle class member modelType = "sports"; System.out.println(modelType+" "+vehicleType); } public static void main(String[] args) { Car car =new Car(); car.showDetail(); } }
Output :
sports Car
Types of Inheritance
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
NOTE: Multiple inheritances is not supported in java
Why multiple inheritances are not supported in Java
- To remove ambiguity.
- To provide a more maintainable and clear design.
super keyword
In Java, the
super
keyword is used to refer to the immediate parent class of a class. In other words, the super keyword is used by a subclass whenever it needs to refer to its immediate superclass.
Example of Child class referring Parent class property
using super
keyword
class Parent { String name; } public class Child extends Parent { String name; public void details() { super.name = "Parent"; //refers to parent class member name = "Child"; System.out.println(super.name+" and "+name); } public static void main(String[] args) { Child cobj = new Child(); cobj.details(); } }
Output :
Parent and Child
Example of Child class referring Parent class methods
using super
keyword
class Parent { String name; public void details() { name = "Parent"; System.out.println(name); } } public class Child extends Parent { String name; public void details() { super.details(); //calling Parent class details() method name = "Child"; System.out.println(name); } public static void main(String[] args) { Child cobj = new Child(); cobj.details(); } }Output :Parent Child
Example of Child class calling Parent class constructor
using super
keyword
class Parent { String name; public Parent(String n) { name = n; } } public class Child extends Parent { String name; public Child(String n1, String n2) { super(n1); //passing argument to parent class constructor this.name = n2; } public void details() { System.out.println(super.name+" and "+name); } public static void main(String[] args) { Child cobj = new Child("Parent","Child"); cobj.details(); } }
Output :
Parent and Child
Superclass reference pointing to a Subclass object.
In context to the above example where Class B extends class A.
A a=new B();
is legal syntax because of IS-A relationship is there between class A and Class B.
Q. Can you use both this() and super() in a Constructor?
NO, because both super() and this() must be the first statement inside a constructor. Hence we cannot use them together.
Comments
Post a Comment