| Author |
HAS-A relationship problem
|
Nitesh Nandwana
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
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 ?
|
 |
Nitesh Nandwana
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
Please bartenders i am still waiting.Even after 1 day i didn't get my answer, where is my answer ?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3865
|
|
PatienceIsAVirtue. Everyone here is a volunteer, you can't just demand responses.
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
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
Sorry matthew i'll remember that rule
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 Nandwana
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
thank you matthew
|
 |
Riza Aktunc
Greenhorn
Joined: Sep 07, 2011
Posts: 24
|
|
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.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Eli Wood
Ranch Hand
Joined: Sep 04, 2008
Posts: 37
|
|
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
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
fred rosenberger wrote:
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.
May i become a bartender and how to become bartender ?
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
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.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
Nitesh Nandwana wrote:May i become a bartender and how to become bartender ?
see this
|
 |
Nitesh Nandwana
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
fred rosenberger wrote:
Nitesh Nandwana wrote:May i become a bartender and how to become bartender ?
see this
thanky you fred i'll help here everyone to get bartender job.
|
 |
Nitesh Nandwana
Ranch Hand
Joined: Jun 07, 2011
Posts: 115
|
|
Eli Wood wrote:
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.
thank you Eli such a nice explanation
|
 |
 |
|
|
subject: HAS-A relationship problem
|
|
|