File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the use of Inheritance

 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic