aspose file tools*
The moose likes Java in General and the fly likes Why need of inheritance??????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why need of inheritance???????" Watch "Why need of inheritance???????" New topic
Author

Why need of inheritance???????

Aadi Narayana Reddy
Greenhorn

Joined: Oct 17, 2005
Posts: 13
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.


G.Adi Narayana Reddy,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: Oct 17, 2005
Posts: 13
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why need of inheritance???????