Hi all,
for the below code:

class A{ }
class B extends A{ }

why we extends 'A' class to inherit the properties in 'B'?
Here we can use, 

class B{
   A a = new A();//to get properties of 'A' class
}

why should we go for inheritance by using 'extends' keyword to inherit?
Please help me out in this concept. Thanks in advance.

Recommended Answers

All 4 Replies

surya: this is like asking, why should your parents have had children, since they were there already?

the childclasses don't just inherit the code from the parent class, they are able to add to it, to update it, or to modify it to adjust to a new situation, without changing the original class, so that existing software won't break, or won't have to be re-written in order to be able to run without having changes in the functionality.

in the example you post, indeed, it makes no sense to inherit the parent class, nor does it make much sense to create the subclass, for that matter, since you're not doing anything with them.

it's like bundling a lot of empty pages and selling them as a bestseller novel. it only becomes an actual book, by the letters, words and sentences, basically, the contents the author puts in it.

now, not every developer is able to write bestselling novels, but each and every developer is an author, not of books, but of code. and that code only makes sense when you put some actual code in it.

follow Taywin's link, read up in a book, and look around, try to imagine the OO principles in your daily life: let's say there is a base class 'Chair'. now, when I say chair, you can imagine something, but it might be that I'm talking about something very different then you imagine. Chair, however, can be, ... specified, to subcategories, subclasses, if you may:

GardenChair extends Chair
OfficeChair extends Chair
BarStool extends Chair
KitchenChair extends Chair
CarSeat extends Chair

now, imagine yourself each and every type of these chairs. Think what they have in common, and in which way they are different.

for the common information: there is no need to write duplicate the code in all those classes, you just put that in the baseclass, which is inherited by all the childclasses.
only the code that is specific to each type, is to be written in that class.

extends keyword is part of the syntax showing that you are inheriting from a class. Why would you do that? Although you can go without it, once a smart person said, don't reinvate the wheel once it has been. With that said, no point of rewriting an existing code while you could use something that is already done. The example with the chair is very good. A more human one probably is about mammals. All mammals have common things, eyes, legs, arms, organs etc, we all move, jump etc but we do it in different ways. Instead of explaining in every class the same information we can just inherit it from a class that already has defined it. Further, in other languages such as c++, inheriting from multiple classes is allowed, while in java it is not. A walk around is to an interface, as many classes can inherit from a single interface

Slavi: extending multiple classes and implementing multiple interfaces is not the same, it's not a 'walk around' since it doesn't achieve the same result.

(even though since Java 8 it's possible to put default methods in interfaces)

you'll have polymorphism with multiple types, but still no multiple inheritance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.