HAS-A relationship problem

1.if we can use instance variables and methods of other classes using HAS-A relationship then why inheritance was needed ?

2.As well as what are the rules with HAS-A relationship like access modifiers affect on HAS-A relationship ? as it affects on inheritance.What will be affect on HAS-A relationship if access modifiers of class member of other class are private or protected ?

Personally, I didn't answer the first time I saw it because I'm not sure I understand the question. So this may not be what you're looking for:

1. Inheritance is used for a completely different type of relationship (IS-A), so the fact that you can implement HAS-A with instance variables is irrelevant. Sometimes you want IS-A.

2. When you're using instance variables, you're accessing from a different class, so the rules are the same as any other time you access a different class. private members are inaccessible, protected/default members are accessible from the same package.

Nitesh Nandwana wrote:i thought about need of HAS-A relationship is only one that is, to allow more specialist classes usage hence reduction in code else i could directly write show() in Car class.

Sometimes, the subclass directly uses the method of the superclass without overriding it.
Think about a Rectangle and Shape classes. Rectangle do not need to override getColor() and setColor() methods of Shape class, instead just extends the Shape class. Moreover, it can directly access Shape class'es instance variables such as Color.
If you use HAS-A instead of IS-A, you cannot use polymorphism,either.

My point is that IS-A is not redundant and it indeed reduces the code size and enable code reusability. In fact, almost all object oriented design paterns include IS-A relationship.

Nitesh Nandwana wrote:Please bartenders i am still waiting.Even after 1 day i didn't get my answer, where is my answer ?

Further, you seem to have a mis-conception of what a bartender's job is here. They are NOT required to answer any posts. In fact, they may not even have any knowledge of java at all. Their job is to enforce the rules.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Nitesh Nandwana wrote:Matthew i thought about need of HAS-A relationship is only one that is, to allow more specialist classes usage hence reduction in code else i could directly write show() in Car class.

Nitesh, it is true that both the IS-A and HAS-A relationships can reduce the amount of code we write. In my opinion, though, the main benefit relates to the principle of cohesion and the ability to write cohesive classes. You will likely find references to the principle of cohesion in your study materials; it basically means that a single class should provide only closely related functionality.

Here is a silly example I coded up. You will see that the UncohesiveCar class, in order to provide required functionality without its objects HAVING a Person, must provide methods that are not closely related to cars. It is not cohesive. Since objects from the Car class HAVE a Person passenger, that method can be pushed into the Person class, where it belongs.

Nitesh Nandwana wrote:Please bartenders i am still waiting.Even after 1 day i didn't get my answer, where is my answer ?

Further, you seem to have a mis-conception of what a bartender's job is here. They are NOT required to answer any posts. In fact, they may not even have any knowledge of java at all. Their job is to enforce the rules.

Suppose you have a family of Car classes and you want to include Coupes and Sedans. Without the Car class to extend, that confers Car behavior on its subclasses, you would have to create a bunch of car behavior classes and declare them in each car class you defined. All cars will need accelerating and braking behavior. All cars will need steering behavior. This can be accomplished with HAS-A, but is more concisely, and IMO expressively, rendered with IS-A.

Nitesh Nandwana wrote:Matthew i thought about need of HAS-A relationship is only one that is, to allow more specialist classes usage hence reduction in code else i could directly write show() in Car class.

Nitesh, it is true that both the IS-A and HAS-A relationships can reduce the amount of code we write. In my opinion, though, the main benefit relates to the principle of cohesion and the ability to write cohesive classes. You will likely find references to the principle of cohesion in your study materials; it basically means that a single class should provide only closely related functionality.

Here is a silly example I coded up. You will see that the UncohesiveCar class, in order to provide required functionality without its objects HAVING a Person, must provide methods that are not closely related to cars. It is not cohesive. Since objects from the Car class HAVE a Person passenger, that method can be pushed into the Person class, where it belongs.