aspose file tools*
The moose likes Beginning Java and the fly likes What is the use of Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use of Inheritance" Watch "What is the use of Inheritance" New topic
Author

What is the use of Inheritance

Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Please explain me what is the use of inheritance, If i can use the functionality of base class by making object to the base class in our derived class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Inheritance is one of the fundamental features of object-oriented programming. It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common superclass (although it's not a good idea to create a superclass just for the sake of sharing code - class designs should not break the Liskov substitution principle).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Jesper Young wrote:Inheritance is one of the fundamental features of object-oriented programming. It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common superclass (although it's not a good idea to create a superclass just for the sake of sharing code - class designs should not break the Liskov substitution principle).



Sir,
First Thanks for reply
But i have some confusion that if we talking about the polymorphism (Overloading and Overriding), we do overloading in same class and we can do it without inheritance and in Overriding we define our own method which overlap the method of super class. then what is the use of inherit a class.
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
if you have a class Car with some general methods and fields that all cars have... for example brake,sterring,wheel,etc..

now if you do code for AmbassadorCar, you need not to do the same thing which is already done in Car class, you can make use of that..(re-inventing the same thing is foolish)...

now if do code for BenzCar, you will have some extra feature of everything what normal has...i.e you will have steering here also but that may power steering,then hand brake etc..also you can add some more things here like AC,MP3 player etc...

Finally what i am trying to tell you is Super class will be more general.. in the sub class you can give specific implementation to the super class methods...
Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Prakash Attur wrote:if you have a class Car with some general methods and fields that all cars have... for example brake,sterring,wheel,etc..

now if you do code for AmbassadorCar, you need not to do the same thing which is already done in Car class, you can make use of that..(re-inventing the same thing is foolish)...

now if do code for BenzCar, you will have some extra feature of everything what normal has...i.e you will have steering here also but that may power steering,then hand brake etc..also you can add some more things here like AC,MP3 player etc...

Finally what i am trying to tell you is Super class will be more general.. in the sub class you can give specific implementation to the super class methods...




Dear Sir,

My Question is that i can implements the method or properties of CAR Class in my AmbassadorCar subclass be Making the object of CAR Class and it will work so what is the Use of inherit Car Class.
Colin Wright
Greenhorn

Joined: Apr 21, 2010
Posts: 8
The point is that the child object will have all the properties and methods of the parent, plus some the parent does not have. A child can also overide some parent methods with it's own version.

Look at how SWING controls work in JAVA for some practical examples.

In the example you have been given the implication is that not only does AmbassadorCar have all the properties and methods of Car but it also has some methods / Properties that CAR does not have, it extends what is available from an Object of type CAR.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of Inheritance