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?

Related Questions

Why you need to use inheritance?

Inheritance is an object oriented programming concept that helps us with the following benefits:You can reuse existing code instead of having to write them againredundancy of code is avoidedrework and maintenance is easy


what helps prevent errors in DNA replication?

DNA polymerase checks the DNA for errors.


What is the significance of using a double semi in programming languages?

Using a double semi-colon in programming languages, also known as a double semi, is significant because it is used to end a statement or line of code. This helps to clearly separate different parts of the code and make it easier for the programmer to read and understand. It also helps to prevent errors and ensure that the code runs smoothly.


Why is a string immutable in programming languages?

A string is immutable in programming languages because once it is created, its value cannot be changed. This helps ensure data integrity and prevents accidental modifications that could lead to errors in the program.


What is the significance of atomic operations in concurrent programming?

Atomic operations are important in concurrent programming because they ensure that certain operations are executed as a single, indivisible unit. This helps prevent race conditions and ensures data integrity in multi-threaded environments.


Advantages of computer programming?

The advantages of computer programming is that it pays well since programming is a creative task. Computer programming helps human beings solve problems on a regular basis.


What is the meaning of the word programming?

The definition of the word "Programming" is "the action or process of writing computer programs." Hope this helps.


What does the term "atomic" mean in programming and how does it impact the execution of code?

In programming, the term "atomic" refers to an operation that is indivisible and cannot be interrupted. This impacts the execution of code by ensuring that certain operations are completed without interference from other processes, which helps maintain data integrity and prevent race conditions.


Knowing the difference between the niprnet and siprnet helps prevent what?

It helps to prevent Spillage.


Why do you have to focus on object oriented program?

Well, it certainly makes programming much, much easier. It is confusing at first, but it helps a lot, once you master it.Well, it certainly makes programming much, much easier. It is confusing at first, but it helps a lot, once you master it.Well, it certainly makes programming much, much easier. It is confusing at first, but it helps a lot, once you master it.Well, it certainly makes programming much, much easier. It is confusing at first, but it helps a lot, once you master it.


How can binary search prevent overflow in a program?

Binary search can prevent overflow in a program by efficiently dividing the search space in half at each step, reducing the number of comparisons needed. This helps prevent the program from running out of memory or exceeding its capacity, which can lead to overflow errors.


What is testing in computer programming?

In computer programming, testing is the process of evaluating software to identify any potential defects or errors. It typically involves running the program with different inputs to verify that it behaves as expected and meets the specified requirements. Testing helps ensure the quality and reliability of the software before it is released to end users.