Why subclass & superclass in OOP?

조회 수: 8 (최근 30일)
J. Hu
J. Hu 2013년 10월 11일
댓글: Cedric 2013년 10월 11일
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

채택된 답변

Cedric
Cedric 2013년 10월 11일
편집: Cedric 2013년 10월 11일
I am not sure that the room should be a super class for the desk actually, in the sense that the desk is not a specific type of room. Now technically nothing prevents you to implement your first idea. Let me propose another approach though, that should be more useful for illustrating how OOP works. We start with a class Furniture that we will use as superclass for classes representing more specific pieces of furniture in the room (superclasses are less specific than their subclasses).
classdef Furniture
properties
type, color, height, width, depth
end
methods
function obj = Furniture( color, height, width, depth, type )
% Method FURNITURE (constructor)
obj.color = color ;
obj.height = height ;
obj.width = width ;
obj.depth = depth ;
if nargin < 5
obj.type = 'generic' ;
else
obj.type = type ;
end
end
end
end
Now we create a subclass Desk, which is more specific than Furniture:
classdef Desk < Furniture
properties
nDrawers
end
methods
function obj = Desk( color, height, width, depth, nDrawers )
% Method DESK (constructor)
% - Call superclass constructor.
obj = obj@Furniture( color, height, width, depth, 'desk' ) ;
% - Define Desk specific properties.
obj.nDrawers = nDrawers ;
end
end
end
Inheritance provides Desk with all the properties/methods of Furniture, and you can see that we add one property to Desk, which is the number of drawers. Now we can create desks
>> d1 = Desk( 'brown', 1, 2, 1, 7 ) ;
>> d2 = Desk( 'black', 1.5, 3, 1, 10 ) ;
and display their properties (observe that properties of Furniture are there)
>> d1
d1 =
Desk
Properties:
nDrawers: 7
type: 'desk'
color: 'brown'
height: 1
width: 2
depth: 1
>> d2
d2 =
Desk
Properties:
nDrawers: 10
type: 'desk'
color: 'black'
height: 1.5000
width: 3
depth: 1
Let's now do the same for classrooms. We start by defining the more generic class Room, as follows:
classdef Room < handle
properties
type, surfaceArea, hasWindow, hasElectricity, furnitureList
end
methods
function obj = Room( surfaceArea, hasWindow, hasElectricity, type )
% Method ROOM (constructor)
obj.surfaceArea = surfaceArea ;
obj.hasWindow = hasWindow ;
obj.hasElectricity = hasElectricity ;
obj.furnitureList = {} ;
if nargin < 4
obj.type = 'generic' ;
else
obj.type = type ;
end
end
function fId = addFurniture( obj, furniture )
% Method ADDFURNITURE
fId = numel( obj.furnitureList ) + 1 ;
obj.furnitureList{fId} = furniture ;
end
end
end
As you can see, I added a method for adding furniture to the room object. Now we define a more specific class Classroom:
classdef Classroom < Room
properties
hasBlackboard
end
methods
function obj = Classroom( surfaceArea, hasWindow, hasElectricity, hasBlackboard )
% Method CLASSROOM (constructor)
% - Call superclass constructor.
obj = obj@Room( surfaceArea, hasWindow, hasElectricity, 'classroom' ) ;
% - Define Classroom specific properties.
obj.hasBlackboard = hasBlackboard ;
end
end
end
As you can see, Classroom is more specific than Room, because it inherits all properties of Room and has one extra property hasBlackboard. Classroom also inherits the method addFurniture of Rooom. At this point we can create a classroom..
>> cr = Classroom( 100, true, true, true ) ;
and display its properties..
>> cr
cr =
Classroom handle
Properties:
hasBlackboard: 1
type: 'classroom'
surfaceArea: 100
hasWindow: 1
hasElectricity: 1
furnitureList: {}
For the moment it has no furniture, so the property furnitureList is an empty cell. Let's add the two desks that we created previously and display again properties..
>> cr.addFurniture( d1 ) ;
>> cr.addFurniture( d2 ) ;
>> cr
cr =
Classroom handle
Properties:
hasBlackboard: 1
type: 'classroom'
surfaceArea: 100
hasWindow: 1
hasElectricity: 1
furnitureList: {[1x1 Desk] [1x1 Desk]}
Now the two desks are stored in furnitureList. We can display their properties, just to check..
>> cr.furnitureList{1}
ans =
Desk
Properties:
nDrawers: 7
type: 'desk'
color: 'brown'
height: 1
width: 2
depth: 1
>> cr.furnitureList{2}
ans =
Desk
Properties:
nDrawers: 10
type: 'desk'
color: 'black'
height: 1.5000
width: 3
depth: 1
Ok, it works! As you can see, Desk is not a subclass of Room here, but of the more generic (hence superclass) class Furniture. Similarly, Classroom is a subclass of the more generic class Room. Then Room has a property furnitureList for storing furniture objects, but there is no sub/superclass relationship between Furniture or Desk and Room or Classroom.
For more information, please refer to the official doc about OOP in MATLAB. I wrote this little example just to help you starting with the material.
  댓글 수: 2
J. Hu
J. Hu 2013년 10월 11일
Awesome. Thanks a lot...
Cedric
Cedric 2013년 10월 11일
You're welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2013년 10월 11일
편집: per isakson 2013년 10월 11일
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.
  댓글 수: 1
J. Hu
J. Hu 2013년 10월 11일
Got you. subclass has to a specialization of its superclass. Thanks...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by