Inheritance in Java Everything You Need to Know

Inheritance is an of import pillar of OOP(Object-Oriented Programming). It is the machinery in java by which one class is allowed to inherit the features(fields and methods) of some other course.

Important terminology:

  • Super Form: The class whose features are inherited is known as superclass(or a base form or a parent class).
  • Sub Class: The course that inherits the other grade is known as a bracket(or a derived course, extended class, or child class). The subclass can add its ain fields and methods in improver to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of "reusability", i.e. when we desire to create a new form and there is already a class that includes some of the code that we want, we can derive our new class from the existing form. By doing this, nosotros are reusing the fields and methods of the existing class.

How to use inheritance in Java

The keyword used for inheritance is extends.

Syntax :

form derived-grade extends base-course   {      //methods and fields   }        

Example: In the below case of inheritance, course Bicycle is a base grade, class MountainBike is a derived class that extends Bicycle class and class Test is a driver course to run program.

Coffee

class Bike {

public int gear;

public int speed;

public Cycle( int gear, int speed)

{

this .gear = gear;

this .speed = speed;

}

public void applyBrake( int decrement)

{

speed -= decrement;

}

public void speedUp( int increase)

{

speed += increase;

}

public String toString()

{

render ( "No of gears are " + gear + "\n"

+ "speed of bicycle is " + speed);

}

}

course MountainBike extends Bicycle {

public int seatHeight;

public MountainBike( int gear, int speed,

int startHeight)

{

super (gear, speed);

seatHeight = startHeight;

}

public void setHeight( int newValue)

{

seatHeight = newValue;

}

@Override public String toString()

{

return ( super .toString() + "\nseat height is "

+ seatHeight);

}

}

public grade Examination {

public static void main(String args[])

{

MountainBike mb = new MountainBike( iii , 100 , 25 );

System.out.println(mb.toString());

}

}

Output

No of gears are 3 speed of wheel is 100 seat peak is 25          

In the above program, when an object of MountainBike grade is created, a re-create of all methods and fields of the superclass larn memory in this object. That is why by using the object of the subclass we can also access the members of a superclass.

Delight note that during inheritance simply the object of the subclass is created, not the superclass. For more, refer Coffee Object Creation of Inherited Class.

Illustrative image of the plan:

f

In practice, inheritance and polymorphism are used together in java to achieve fast performance and readability of code.

Types of Inheritance in Coffee

Below are the dissimilar types of inheritance which are supported by Java.

1. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves every bit a base of operations class for the derived form B.

Java

import coffee.io.*;

import java.lang.*;

import java.util.*;

class ane {

public void print_geek()

{

System.out.println( "Geeks" );

}

}

class two extends one {

public void print_for() { System.out.println( "for" ); }

}

public class Main {

public static void main(Cord[] args)

{

two g = new two();

one thousand.print_geek();

chiliad.print_for();

g.print_geek();

}

}

Single_Inheritance

2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and likewise as the derived grade also human activity as the base class to other class. In the beneath image, form A serves every bit a base course for the derived class B, which in turn serves every bit a base class for the derived class C. In Java, a class cannot straight access the grandparent'due south members.

Java

import java.io.*;

import java.lang.*;

import java.util.*;

class 1 {

public void print_geek()

{

Organisation.out.println( "Geeks" );

}

}

class ii extends one {

public void print_for() { System.out.println( "for" ); }

}

class three extends two {

public void print_geek()

{

System.out.println( "Geeks" );

}

}

public course Main {

public static void main(Cord[] args)

{

three yard = new iii();

chiliad.print_geek();

g.print_for();

g.print_geek();

}

}

Multilevel_Inheritance

3. Hierarchical Inheritance: In Hierarchical Inheritance, one course serves every bit a superclass (base grade) for more than one subclass. In the below image, grade A serves as a base class for the derived grade B, C and D.

Java

class A {

public void print_A() { Arrangement.out.println( "Form A" ); }

}

class B extends A {

public void print_B() { Organisation.out.println( "Class B" ); }

}

class C extends A {

public void print_C() { System.out.println( "Course C" ); }

}

form D extends A {

public void print_D() { System.out.println( "Form D" ); }

}

public class Exam {

public static void main(String[] args)

{

B obj_B = new B();

obj_B.print_A();

obj_B.print_B();

C obj_C = new C();

obj_C.print_A();

obj_C.print_C();

D obj_D = new D();

obj_D.print_A();

obj_D.print_D();

}

}

Output

Course A Class B Class A Class C Class A Class D          

Hierarchical Inheritance

four. Multiple Inheritance (Through Interfaces): In Multiple inheritances, one form can take more than than one superclass and inherit features from all parent classes. Delight note that Java does not back up multiple inheritances with classes. In java, we can accomplish multiple inheritances only through Interfaces. In the paradigm below, Course C is derived from interface A and B.

Java

import java.io.*;

import java.lang.*;

import java.util.*;

interface one {

public void print_geek();

}

interface 2 {

public void print_for();

}

interface three extends 1, ii {

public void print_geek();

}

course child implements three {

@Override public void print_geek()

{

System.out.println( "Geeks" );

}

public void print_for() { System.out.println( "for" ); }

}

public form Main {

public static void master(String[] args)

{

child c = new child();

c.print_geek();

c.print_for();

c.print_geek();

}

}

Multiple_Inheritance

5. Hybrid Inheritance(Through Interfaces): Information technology is a mix of ii or more of the in a higher place types of inheritance. Since coffee doesn't support multiple inheritances with classes, hybrid inheritance is also non possible with classes. In coffee, we tin achieve hybrid inheritance only through Interfaces.

hybrid

Important facts nearly inheritance in Coffee

  • Default superclass: Except Object class, which has no superclass, every course has i and only one directly superclass (single inheritance). In the absenteeism of any other explicit superclass, every form is implicitly a subclass of the Object class.
  • Superclass can only be one: A superclass can have any number of subclasses. Just a subclass tin can have only one superclass. This is considering Java does not support multiple inheritances with classes. Although with interfaces, multiple inheritances are supported by coffee.
  • Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, and so they are not inherited past subclasses, but the constructor of the superclass can be invoked from the bracket.
  • Individual member inheritance: A subclass does not inherit the private members of its parent grade. However, if the superclass has public or protected methods(like getters and setters) for accessing its individual fields, these can also be used past the subclass.

Java IS-A type of Relationship.

IS-A is a manner of saying: This object is a type of that object. Permit us see how the extends keyword is used to achieve inheritance.

Java

public class SolarSystem {

}

public class Earth extends SolarSystem {

}

public class Mars extends SolarSystem {

}

public class Moon extends Earth {

}

At present, based on the above example, in Object-Oriented terms, the following are true:-

  1. SolarSystem the superclass of Earth class.
  2. SolarSystem the superclass of Mars class.
  3. Earth and Mars are subclasses of SolarSystem grade.
  4. Moon is the subclass of both Earth and SolarSystem classes.

Java

class SolarSystem {

}

class Earth extends SolarSystem {

}

class Mars extends SolarSystem {

}

public class Moon extends Earth {

public static void primary(Cord args[])

{

SolarSystem s = new SolarSystem();

World east = new Globe();

Mars m = new Mars();

System.out.println(south instanceof SolarSystem);

Arrangement.out.println(e instanceof Earth);

Arrangement.out.println(thousand instanceof SolarSystem);

}

}

What all can be washed in a Subclass?

In sub-classes nosotros tin can inherit members as is, replace them, hibernate them, or supplement them with new members:

  • The inherited fields can be used direct, just like whatever other fields.
  • We can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can exist used directly as they are.
  • Nosotros tin can write a new instance method in the bracket that has the same signature as the one in the superclass, thus overriding it (as in the example above, toString() method is overridden).
  • We can write a new static method in the subclass that has the same signature equally the one in the superclass, thus hiding it.
  • We can declare new methods in the subclass that are non in the superclass.
  • We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

learmonthwhince.blogspot.com

Source: https://www.geeksforgeeks.org/inheritance-in-java/

0 Response to "Inheritance in Java Everything You Need to Know"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel