Why subclass & superclass in OOP?
Show older comments
I am a beginner in OOP. So far I found it very powerful. However, there is one concept, subclass & superclass, I do not understand very well. And do not know how to implement it but I am thinking I can utilize for my problem.
Could somebody explain to me with some example? What are the advantages?
Here is the problem I am dealing with. It is o describe objects in a room which contains many objects(child) like desks, tables, and other furniture. The room and the furniture have their own properties. I use a class for each of them as shown below. Calculations are to be done using the properties.
Some calculation to be done for subclass (child class like desk) needs to use properties of the superclass room. Some calculation to be done for superclass (child class like desk) needs to use properties of the subclass room.
So I am thinking the advantage of defining this type super/sub class relation is to help the reference to the properties of each other. What are other good applications. Otherwise, I can just define them as parallel class and include each other as input argument for function definitions. What difference does it, declaring super/subclass, make?
Thanks...
classdef room
properties
length
width
depth
end
end;
classdef desk < room
properties
size
color
weight
end
end
Accepted Answer
More Answers (1)
per isakson
on 11 Oct 2013
Edited: per isakson
on 11 Oct 2013
1 vote
Your question requires more than an answer here.
See the Matlab documentation.
There are many introductions to oop on the net (however, not Matlab specific). Google.
The first two thirds of this might be helpful: Introduction to Object Oriented Programming Concepts (OOP) and More. See especially
- "What is the difference between Association, Aggregation and Composition?".
- "What is Inheritance?"
Understand "is-a" and "has-a".
desk is not a specialization of room; not a type of room; thus not an appropriate subclass of room.
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!