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

Why need of inheritance???????

 
Aadi Narayana Reddy
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This question is regarding the concept of inheritance!
Suppose that there are 2 cases
CASE 1:

class A {
// some members in this class
}
class B extends A {
// some members in this class
}
here we are able to get all the members of the class A in Class B.


CASE 2:

class A {
// some members in this class
}

class B {
//some members in this class
//create object to the class A here
A ob = new A();
}
In this case we are able to get the all members available in class A thro its object available in B.
Then why we need Inheritance???

pls send me some reasons for using inheritance.
 
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
Case 1 is inheritance, case 2 is composition.

Do you understand exactly what inheritance means? It implies an "is a" relationship: if class B extends class A, then an object of type B is an object of type A. Sometimes you want this kind of relationship in your software.

You can for example have a class Vehicle, with subclasses Car and Airplane. You program everything that's common for any kind of vehicle in class Vehicle, and everything that's specific for a specific kind of vehicle in the subclasses Car and Airplane.

Now you might have some operations in your program that work with any kind of vehicle. So you write a method that works on objects of type Vehicle, instead of Car or Airplane:

public void someOperation(Vehicle v) {
// ....
}

Because of inheritance, a Car is a Vehicle, so you can pass a Car object (or an Airplane object) directly to this method:

Car myCar = new Car();
someOperation(myCar);

That wouldn't be possible with composition (case 2).
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are bound to have been discussions about this topic already.
There have also been discussions about abstract classes, and about how abstract classes differ from interfaces on these bulletin boards in the last couple of days.

Yes, you can get all the members of your superclass via the subclass, provided you haven't given them private access, but not vice versa.
the reason for using inheritance is that you will have lots of objects with commonc characteristics, for example if you have a Person class, you will have fields like name, dateOfBirth, address, etc.
You might extend your Person class to have an Adult class, with additional information eg National Insurance number (in UK) or Social Security number (in USA). Children don't have those data.
You can extend your Adult class to a Convict class with fields like offence, lengthOfSentence and prisonNumber. There are lots of Adults who don't have that information at all.

Now, what happens if there is a change in format of one of those data; those of us who are older can remember that postal codes were introduced in UK in the 1970s, and more recently the US zip code has changed from 5 digits to 9 digits. Or you want to add mobile number as well as phone number?
What you do is to go to the superclass, and change the fields and methods there; if it is done correctly, you can be confident that it will work for all the subclasses as well.

So using inheritance makes it much easier to have several similar classes which share common characteristics, and also to change them.
QED
CR
 
Amar Shrivastava
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only private members.
Members which are hidden and overridden are also not inheritted by the subclass.
The simplest rule to find which memers are inheritted is:

All members of the superclass which can be accessed by their simple names(as if they are the members of this subclass) in the subclass are inheritted.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic