• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the use of Inheritance  RSS feed

 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • 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
Sheriff
Posts: 15865
81
Android IntelliJ IDE Java Scala Spring
  • 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
  • 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
  • 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
  • Likes 1
  • 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
  • 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.
 
Arun Singh Raaj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do understand your doubt. Good One!
You are right that properties of super class can be accessed by subclass through the composition, then why Inheritance?
As i know, to achieve Abstraction(abstract class/inheritance), Polymorphism we have to use Inheritance. Moreover super, this, final concepts belong to the inheritance.
If you use composition, it provides tight coupling while Inheritance provides loose coupling, that is good for an application.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am not convinced that it is necessary to use inheritance to access properties of a superclass; the fields shou‍ld be private anyway and access shou‍ld only be possible via public methods. Neither the keyword this nor final is specific to inheritance; both can be used to control access within a single class. I also disagree that composition entails any sort of tight coupling. If a well‑designed class is a field of another class, all access will be via its public interface, so coupling doesn't come into the question. Remember that a public interface has to be maintained for the entire lifetime of an application (or even for ever), so there is no violation of encapsulation or information hiding like that.
In the last thirty years it has gradually become obvious that inheritance is often difficult to achieve, so the wisdom of the dictum, “favour composition over inheritance,” has become more widely recognised.

The same applies to your similar question here.
 
Arun Singh Raaj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
There shouldn't be any doubt that Abstraction, Encapsulation are important for a large application or just an application. Hence to achieve these features, inheritance is necessary.
I apologize that i added 'this' keyword in my first post. But 'final' definitely relevant to inheritance.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Singh Raaj wrote:Abstraction, Encapsulation are important for a large application or just an application. Hence to achieve these features, inheritance is necessary.

No it's not. Abstraction and encapsulation do not depend on inheritance. On the other hand, inheritance does help support abstractions related to specialization in subclasses and generalization in superclasses.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!