answersLogoWhite

0

Inheritance is an object oriented feature supported by Java wherein the features of one Java class can be inherited/made available in another class. This creates a parent child relationship between these 2 classes. Class Inheritance in java mechanism is used to build new classes from existing classes. The inheritance relationship is transitive: if class x extends class y, then a class z, which extends class x, will also inherit from class y. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.

Example:

public class Parent {

private String name = "Rocky";

public String getName(){

return this.name;

}

}

public class Child extends Parent {

public static void main(String[] args){

System.out.println("Name in Parent is: " + getName());

}

}

Here the getName() method is available only in the parent class but is directly used in the child class because the method is public and is directly accessible to the child class since it has extended the parent class.

Benefits of Inheritance:

One of the key benefits of inheritance is to minimise the amount of duplicate code in an application by sharing common code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refactored to move the common code up to a mutual superclass. This also tends to result in a better organisation of code and smaller, simpler compilation units.

Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass

User Avatar

Wiki User

16y ago

What else can I help you with?

Trending Questions
Why in c language initgraph doesn't work? Who ruled Romans from 600B.C. to 500B.C.? Should one provide 12volts for charging two 6volt batteries which are connected in such a way that cathode of one battery is connected to cathode of other and anode is also connected in similar manner? What people do about nuclear radition? Why did use metro seal for transformer relay? How do you remove bolt anchors from cement? How can the base class data be accessed from database directly using derived class? How do you get a grant for a new boiler? If a primary key is not a unique number for each ID entered in as part of the INSERT command an error message will be displayed.? What is the phase relationship between the input and output signals of the common collector amplifier? What inventions are non patented? What is the name of the process developed by The Software Engineering Institute that addresses 18 key process areas for achieving a certain level of organizational maturity? What component do you remove to isolate the secondary distribution center transformer before connecting the primary power cables? How do you adjust the zero ohms control on the back off ohmmeter? How did wener von siemens get the idea of the elevator? What HTML attribute identifies a link target? The fuse 13 A in a plug is intended to? Why do you think software development is risky? What happens to the output voltage of a rectifier if the capacitor becomes very leaky? What is critical velosity in oilwell drilling?