Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Advantages of Inheritance in java.

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between hashmap and hashtable in java?

1048


What is multi level inheritance in java?

961


What is a boolean used for?

1058


What does this mean java?

1029


Is 0 true or is 1 true?

902


What is a java applet? What is an interface?

1029


Can a class be defined inside an interface?

978


Difference between method overloading and method overriding in java ?

1022


What do you understand by copy constructor in java?

880


What is the purpose of the enableevents() method in java programming?

1081


What is JDBC Driver interface?How can you retrieve data from the ResultSet

2046


What is an immutable class? How to create an immutable class?

1003


Explain the difference between map and flatmap stream operation?

1197


What is a default method?

984


Can we create an object if a class doesn't have any constructor ( not even the default provided by constructor ) ?

1024