Advantages of Inheritance in java.

Answers were Sorted based on User's Feedback



Advantages of Inheritance in java...

Answer / kureshi salman

Inheritance means deriving a child class from a parent class
(i.e.existing class).
Use of inheritance iin java is we can reuse of a code.
For Ex.
class a
{
//variables.....
//methods.......
}
class b extends a
{
//variable of class a....
//methods of class a.....
//also
//variables and methods of class b
}
it means that we can inherit the class and features of
existing class in a new class.

Is This Answer Correct ?    29 Yes 3 No

Advantages of Inheritance in java...

Answer / sitaram

Inheritance: The base class properties into subclass class
is called inheritance.
Inheritance example:Father and child relation. Father
having 10 lakhs property. That property is always avilable
to child. Father is a parent class. child is a subclass.
Advantage: 1.subclasss take the all properties from parent
class.
2.suppose you can add any property in parent class. That
will be effected to subclasses also.

Is This Answer Correct ?    22 Yes 2 No

Advantages of Inheritance in java...

Answer / jadhavpnilesh

I read all the answers and same I explained in my recent
interview. She asked me instead of inheriting the class i
will create object of class and call methods of that class
is it not reuse of code ? What she was expecting exactly,
can anyone help ?

Is This Answer Correct ?    16 Yes 8 No

Advantages of Inheritance in java...

Answer / prema g.v

Inheritance is a object oriented concept where in the subclass acquires all the properties of superclass.

The main use of inheritance are,
1. Code reuse.
2. Code enhancement- built on what you have, without altering the present functionality.

For example: Consider the Class animal,

class animal {
String name;
int size;

public void sleep() {
System.out.println("Animal is sleeping");
}

public void eat() {

System.out.println("animal is eating");
}
}

In this example the class animal has a state name and size and behaviors sleep(), eat().

Now I will create a subclass dog,

class dog extends animal {
int legs;

public void bark() {

System.out.println("Dog barking");
}
}

class cat extends animal {
int legs;

public void make_sound() {

System.out.println("cat making sound miyo miyo");
}
}

Here extends key word indicates inheritance.

The subclasses dog and cat now acquires the properties of class animal i,e the class dog and animal has got state size, name and behaviors sleep(), eat() along with their own state(instance variable) and behavior(method).

Now if we want to invoke sleep() method in the class dog and animal, we can directly invoke by creating dog and cat class objects. It means you need not write functionality of sleep() method in dog and class. We are re-using the code of animal class sleep() method by using inheritance concept. Suppose if we did not have the concept of inheritance, we need to write sleep() method functionality in cat and dog class as well, which is something not good.

Suppose if we want dog to sleep differently than animal, we can also provide our own implementation just by overriding sleep() method in dog class i,e we can enhance the code of sleep() method in dog class. To override methods in subclass we should maintain same signature as that of super class.

i,e in dog class,

public void sleep() {
System.out.println("Dog sleeping with its eyes closed");
System.out.println("dog is sleeping");
}

Here just we are building on what we have, nothing on enhancement.

When ever we encounter IS-A relationship, then its shows inheritance. Example Cat is an animal. Lilly is a flower.

Super classes indicates generalized behavior. Subclasses indicates specialized behavior.

i.e the above example indicates all animal will sleep and eat- indicates generalized behavior. But only dark can bark, this behavior is only related to dog.

All generalized behavior we can put it in superclass and can use it in subclasses -again reuse of code.

Is This Answer Correct ?    8 Yes 0 No

Advantages of Inheritance in java...

Answer / prasanna

java is a object oriented programming language in this
inhertiance is the one of the part. can we say inhertiance
can be use for all classes and it is used for inhert the sub
class from the super class

Is This Answer Correct ?    8 Yes 2 No

Advantages of Inheritance in java...

Answer / josh

Inheritance can lead to a combinatorial explosion of
classes. Composition should be somewhere in the code
oftentimes. For example, an Employee object can "contain" a
PaymentType object, a PaymentSchedule object, and a
PaymentMethod object. Each of those 3 objects are their own
classes. The Employee class creates instance of those
classes and "holds" on to them. From there, a higher class
such as a PayrollSystem can look at an Employee object and
query that Employee about that Employee's information
(namely its object contents). With composition, your code is
highly decoupled, meaning that those 3 object held by
Employee can easily be held by some other class (hence code
reuse). It is important to note that PaymentSchedule,
PaymentMethod, and PaymentType should be interfaces which
have multiple concrete implementations to them (ie. a
PaymentType interface could be implemented with an Hourly,
Salary, or SalaryWithCommission class for a given Employee).
Inheritance causes high coupling and reduces code
reusability. It helps to write a UML static(class) diagram
before typing up code

Is This Answer Correct ?    10 Yes 5 No

Advantages of Inheritance in java...

Answer / prema g.v

Inheritance is a object oriented concept where in the subclass acquires all the properties of superclass.

The main use of inheritance are,
1. Code reuse.
2. Code enhancement- built on what you have, without altering the present functionality.

For example: Consider the Class animal,

class animal {
String name;
int size;

public void sleep() {
System.out.println("Animal is sleeping");
}

public void eat() {

System.out.println("animal is eating");
}
}

In this example the class animal has a state name and size and behaviors sleep(), eat().

Now I will create a subclass dog,

class dog extends animal {
int legs;

public void bark() {

System.out.println("Dog barking");
}
}

class cat extends animal {
int legs;

public void make_sound() {

System.out.println("cat making sound miyo miyo");
}
}

Here extends key word indicates inheritance.

The subclasses dog and cat now acquires the properties of class animal i,e the class dog and animal has got state size, name and behaviors sleep(), eat() along with their
own state(instance variable) and behavior(method).

Now if we want to invoke sleep() method in the class dog
and animal, we can directly invoke by creating dog and cat
class objects. It means you need not write functionality of
sleep() method in dog and class. We are re-using the code
of animal class sleep() method by using inheritance concept.
Suppose if we did not have the concept of inheritance, we
need to write sleep() method functionality in cat and dog
class as well, which is something not good.

Suppose if we want dog to sleep differently than animal,
we can also provide our own implementation just by
overriding sleep() method in dog class i,e we can enhance the code of sleep() method in dog class. To override methods
in subclass we should maintain same signature as that of
super class.

i,e in dog class,

public void sleep() {
System.out.println("Dog sleeping with its eyes closed");
System.out.println("dog is sleeping");
}

Here just we are building on what we have, nothing on enhancement.

When ever we encounter IS-A relationship, then its shows inheritance. Example Cat is an animal. Lilly is a flower.

Super classes indicates generalized behavior. Subclasses indicates specialized behavior.

i.e the above example indicates all animal will sleep and
eat- indicates generalized behavior. But only dark can
bark, this behavior is only related to dog.

All generalized behavior we can put it in superclass and
can use it in subclasses -again reuse of code.

Is This Answer Correct ?    5 Yes 1 No

Advantages of Inheritance in java...

Answer / pari

Minimize the amount of duplicate code in an application

If duplicate code (variable and methods) exists in two related classes, we can refactored that hierarchy by moving that common code up to the common superclass.

Better organization of code

Moving of common code to superclass results in better organization of code.

Code more flexible change

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.

Where to use inheritance:

Before using inheritance first compare the relationship for the two classes. Use inheritance only if there is a parent child relationship is there.
A way to test the relationship is to ask a "is-a" question.

Is This Answer Correct ?    0 Yes 0 No

Advantages of Inheritance in java...

Answer / dude

Recently I use android so why are you teaching java in electronic engineering people

Is This Answer Correct ?    0 Yes 0 No

Advantages of Inheritance in java...

Answer / hitler

Through Inharitance we archiving Dynamic Polymorphism .

Is This Answer Correct ?    5 Yes 11 No

Post New Answer

More Core Java Interview Questions

what is aberivation of java?

14 Answers  


Why java applets are more useful for intranets as compared to internet?

0 Answers  


Can static method access instance variables ?

0 Answers  


What are the two ways of implementing multi-threading in java?

1 Answers  


What is annotation in java?

0 Answers  






What is the meaning of 3 dots in java?

0 Answers  


Is java based on c?

0 Answers  


How many characters is 2 bytes?

0 Answers  


Explain the importance of join() method in thread class?

0 Answers  


What is generic class?

0 Answers   Tech Mahindra,


What is an off by one error in java?

0 Answers  


java is fullu object oriented or pure? why?

8 Answers  


Categories