I'm trying to understand how inheritance with association work between the superclass and its subclasses. Here are the classes with their relationships:
my question here if I created an object from class C, will it inherit also from class A as below? and why?
No. It won't inherit anything. B inherits from A so C will see a B with operations/attributes inherited from A.
If I (class C) use a cleaning device B that inherited from Sponge (A) I will see/use a soft cleaning device. If B inherited from steel it would be steel wool. If inherting from sand it would be sand paper. All cleaning devices with different usability depending from where they inherit. But I wouldn't change in any way. Only the way I would "clean" things. (I know, a horrible example)
N.B. As #www.admiraalit.nl commented your object diagram does not match the class diagram.
Related
I have a class A and class B, class A has a list of instances of class B and class B has a list of instances of class A, in sql language, this is many to many relationship, is there a design pattern that helps in the implementation of these classes or am I overthinking it and it is just as simple as just assigning a respective list for each class?
Thanks
You are overthiking. I don't know why. But you sure are overthing.
For a UML Class Diagram, if a class A has a composition relationship with another class B, does it work like inheritance? I know that the class A cannot exist without class b but does class A inherit the attributes and methods from class B?
Composition over inheritance
In OO design, a common advice is to prefer composition over inheritance. This might mislead to think that there is a relation between these two different concepts:
Inheritance is about a relation between classes.
Composition is a about relations between objects of classes.
Even more misleading: the "composition" used in the general OO literature corresponds in general to a simple UML association and is not to be understood as restrictive as the UML "composition".
Inheritance is about generalisation and specialisation
If class D inherits from B, it means that D specialises a more general concept B:
It has all the properties and operations of B (without need to repeat them in the diagram),
It can have additional properties and operations that are not relevant for B, and
It can perform some operations of B differently.
Any object of class D is also an object of class B for its full lifetime. This is why we speak of a "Is a" relationship.
Naive example (just for the illustration):
Composition is about association of objects of very different classes
If class B "composes" (has a composition association with) C, it means that the B object can be associated with different C objects over its lifetime. Anf that objects of class C :
always belong to a B object that owns it. This is why we sometimes speak of a "has a" relationship;
will not survive its owning B object .
Caution: UML composition is about an exclusive ownership. In the OO literature, the term "composition" often refers to weaker associations without any ownership or even exclusivity.
Naive example (just for the illustration):
Inheritance and composition are two different concepts. If they were the same there would not be the need to have different notation.
Compostion is about lifetime of objects. Composed elements will be destroyed the moment the owner is destroyed. But each instance can (and likely will) be something completely different than the composing object. The well known example is a car: When you shredder a car its composed wheels will be shreddered as well. Only if you detach them beforehand they can live on.
Inheritance means that instances will have operations and attributes on their own defined in the parent class. This works in each single instance. There's no good real worl example for inheritance (genes work different and inherited assets also don't help here).
if a class A has a composition relationship with another class B
Lets suppose you mean A <*>--- B or A <*>---> B rather than the reverse (B <*>--- A or B <*>---> A), because this is not very clear in your question.
class A cannot exist without class b
It is not about classes but about instances of these classes, and B can have instances independently of that composition, so the limited life is not for all instances of B.
does it work like inheritance? does class A inherit the attributes and methods from class B ?
No.
A inherits B means A is a B, this is absolutely not the case having A <*>--- B or A <*>---> B or the reverse.
Then independently of the visibility of the operations you cannot apply the operations of A to the instances of B nor the reverse, except of course if you also have an inheritance more that the composition.
About the attributes of B depending on the way the composition is implemented it is possible they are part of the corresponding instance of A, this is the case for instance in C++ not using a pointer for the composition. But anyway that does not means these attributes of B are attributes of A, to access them it is needed to first access of the instance of B though the compositions, and that also supposes an instance of A has the right to directly access these attributes of B, so they are typically public.
Note also you can have A<*>--->B<*>--->A etc and fortunately that does implies A inherits B and B inherits A which is impossible
I have a subclass which inherits the properties and methods of a super class. However, I want this subclass to only inherit some of the properties from the superclass, not all of them.
In OO programming, is this bad practice? I have noticed that it is impossible to do in ES6 JavaScript.
This is not possible in OOP
If you define B as subclass of A, then B inherits all the properties and methods from A.
There are some exceptions:
private members of A will not be accessible in B (BTW the code will be there)
initializers/constructors are not always automatically inherited, here the rules depend on the language you are using
I'm familiar with the practical aspects of OOP. Now I'm writing a technical document and I'm questioning my use of terms.
Take this class hierarchy. Each class has zero, one, or more (multiple inheritance) classes it directly derives from. For B, this is A. I'd say that A is B's base class or superclass (interchangeably).
Conversely, a class can have any number of classes directly deriving from it. I'd say that B and C are A's derived classes or subclasses, again interchangeably.
Yet there must also be a term to refer to all classes a class directly or indirectly inherits from. How would you call {A, B} from D's perspective?
And I'm missing another term for all classes directly or indirectly derived from a class. How would you call {B, C, D, E} from A's perspective?
Good questions. In this case, A is the root class, as all other classes inherit from it. All objects descend from A, or are either direct or indirect subclasses of A. The opposite is also true. B is a direct superclass of D and A is an indirect superclass. I suppose you could use the word ancestor as the opposite of descend/descendant, but I haven't seen this before.
what is the difference between inheritance and category in objective-c
Both are used for the subclass! So what is difference between them
While Category is a nice way to add functionality to the base class, people like me who come from other object oriented technology such as Flash, will find a little difficult to understand as to how this thing relates to the inheritance chain. The same question came up to my mind and I did a quick research on the topic.
The final thing is Category does the same thing as it tells about itself. It adds functionality to the base class. If you remember this, then there would be no confusion at all.
Well, for that to understand, lets take an example. Suppose there is a Class A and Class B is a subclass of Class A. In the application Class B is used in a lot of places. Now, there is a need to add some more functionality to Class A, so a new category is written as "A+newRole". Once this category is written, the new functionality is added to the base class and in this case, Class A. That means, all those classes which are child classes of Class A such as Class B, automatically gets the functionality. Thats freaking cool. One can straight away go ahead and call the new methods added in the Category from the child classes. The only thing necessary here is to import the Category file to the appropriate place.
A category adds extra functionality to a class without generating a new class at all, you just extend it but it does not have polimorphism implied or anyting like it.
Inheritance on the other hand, generates a new class on its own right in which you can add new instance variables and override behavior from the parent class by polimorphism.