0
class B extends A 

by this code class B can use variables and methods of class A.

class B
{
    A a=new A();
}

Now we can also use variables and methods of class A using object a in class B.

So my question is this----Why we use these two different way to use the other class properties(in term of variables and methods)..

SilentBomb
  • 168
  • 2
  • 11
  • 2
    Don't have time for a full answer, but what you're looking for is [inheritance vs composition](https://www.google.com/search?q=inheritance+vs+composition). Also see [Difference between inheritance and composition](http://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition). – Brendan Long Nov 08 '11 at 05:51

5 Answers5

4

The second method is called delegation. There is also composition - when A is a member of B.

The difference between the two is that if A is not in the same package as B (package access is a different case - left as exercise for the reader) - then in case of delegation or composition you will have access only to public data members and methods of class A, whereas in case of inheritance, the inheriting class B will have access to public and protected members and methods of class A.

Update

There are a lot of additional differences even in the case that all methods and members of A are accessible to B (most of them relate to the case when you are not the owner of both classes, rather when A is provided to you by a third party and you don't have control over its access modifiers).

But even if you do have the control and can define all of the members and methods of A as public - do you want to? It would introduce a horde of additional maintenance issues - most important, IMO, is that anything you define as public is a contract between you and the user of your code - perhaps someone in a different part of the planet. Which in turn means that you can't hide your implementation details, your private utility methods or anything from him - nothing prevents him from using some method, that you wrote only for some inner calculation, directly.

What that means is - when tomorrow you decide that you don't want to maintain that method again or that it introduces some sort of performance or security issue - you can't remove it. Someone already uses it.

Another update

As mentioned below, and something that I haven't mentioned, in the case of inheritance you also receive a "free gift" - polymorphism. See here.

ZenMaster
  • 12,363
  • 5
  • 36
  • 59
  • I like way in which you explain.But both way results for same things..But if both class A and class B is in same package and declare public, then what will be the difference.\ – SilentBomb Nov 08 '11 at 05:57
  • 1
    To add to this, with inheritance you can also use type B anywhere that type A is used. In the second example shown, even though it contains an object of type A, it cannot be used for everything object A can be used for, which defeats the purpose of inheritance. – Toast Nov 08 '11 at 06:03
1

I think class B extends A introduce some kind of hierachy, you can use A's methods, variables in B even if you dont know A; and also it is more beautiful in terms of object-oriented design.

James.Xu
  • 8,249
  • 5
  • 25
  • 36
  • 1
    **beautiful** is most certainly not a valid claim, and is probably objective. It also introduces a lot of additional concerns into correct design. – ZenMaster Nov 08 '11 at 05:54
  • What is mean by "if you dont know A".Can you explain – SilentBomb Nov 08 '11 at 05:58
  • e.g. A <- B <- C , you can use all of A's and B's methods in C, you dont need to know which method is in A, which method is in B and call them correspondently. – James.Xu Nov 08 '11 at 06:03
1

One clear difference is overriding of functions defined in class A.

Imagine you have 10 functions that are defined in A and you want to override just one of them. Then, if you use inheritance you override just the one and can still call all 10. With your other approach you can't call any unless you re-implement them. (Or make a public and call it directly, but this is against encapsulation).

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
1

The first example is inheritance while the second is called composition.

Inheritance best represents the "is a" relationship, when B is a more particular kind of entity than A. For example, a car is a kind of vehicle. Inheritance gives you all the public and protected methods and variables of the super-class. It follows that the sub-class should have a logical implementation of each of those inherited traits. For example, a vehicle can have a color, and thus all cars must also have a color.

Composition best represents the "has a" relationship, when A is a sub component of B. For example, a car has an engine. Neither is truly a superset of the other, they are entirely different classes. Yet the car must use the engine to perform. It is unlikely that they would share many methods and variables, so inheritance would be awkward. Imagine the car class having to implement or expose methods for many specific engine details such as oil level and temperature. The car object should not have to expose all these methods to simply... drive().

3martini
  • 510
  • 2
  • 13
1

The first example is an example of an is-a relationship. class B will be an extension of class A and contain all of class A's public members, as well as have access to the protected ones also (which you won't in the second example). This also allows you to group objects derived from class A together (including types of class A), enabling polymorphism with any overridden methods. That's another thing the second example won't allow you to do, override methods defined in class A to make them more suitable for class B. If you want to inherit behaviour from a class, you would do this.

The second is an example of a has-a relationship. You would use that for example in a case where you might have a class Parent, which has-a class Child.

There are plenty of books/guides/documentations that'll provide you with the differences and benefits of each option, and which is best for a particular scenario.

EDIT: I took too long to post, if you search for "is-a" and "has-a" relationships you'll find plenty of information.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44