• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Why need of inheritance?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 80214
423
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 240
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inheritance provides accessibility of parent class' properties.  Abstraction(interface, abstract class), Polymorphism, Loose coupling can be achieved by only Inheritance. Moreover this, super, final keywords belong to Inheritance.
 
Campbell Ritchie
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see this reply.
reply
    Bookmark Topic Watch Topic
  • New Topic