INHERITANCE REVIEW

advertisement
Karel J Robot – L4 to L7 Summary
Name
MAIN CONCEPTS OF OBJECT ORIENTED PROGRAMMING
 An object oriented program consists of a group of objects, exchanging messages, for the purpose of achieving 1
common goal. (ie: group of robots working together to build a house)
 A class is a "blueprint" for making an object.
 An object is an instance of a class.
 All objects are born with 2 parts:
1) instance variables ("data", "what it can remember") ie: street, avenue
2) methods ("things it can do") ie: move, turnLeft
 The 3 most important concepts("pillars") to object oriented programming are:
- Polymorphism, Inheritance, Encapsulation
(remember as “PIE”)
3 PARTS TO A CLASS
1. Instance Variables – store object data. We mark them private to enforce encapsulation
2. Constructor – create and setup the object (initializes all instance variables)
3. Methods – define things an object can do
ENCAPSULATION
 Encapsulation is the process of hiding all details of a class that are not necessary to understanding how objects of the
class are used. Basically, this means build your class in a way that any programmer who wants to use it will know
how to do so, but won't be bothered (ie: need to know) with any coding details of the class.
 Encapsulation divides your class into 2 parts (by using public and private):
A. User Interface – tells other programmers how to use your class (includes all public methods)
B. Implementation (data + code) – the inner workings of the class which is hidden from others
INHERITANCE
 When designing with inheritance, you build 2 types of classes:
1. superclass - put common code here (aka the parent, the more general class)
2. subclass - put specialized/different code here (aka child of parent)
 The subclass inherits all methods(actually only the public ones) from the superclass and it can override any that it
wants and also add additional methods of its own.
BENEFITS OF INHERITANCE
 Inheritance allows you to avoid duplicate code in subclasses (ie: code reuse). This means code is localized to 1
class and it makes it easier to modify in the future (ie: modify in 1 class only(superclass), not all subclasses)
Determining Which Method is Called with Inheritance
 When you send a message to an object, you use the most specific version of the message (ie: lowest in tree)
 Suppose you have the following inheritance tree:
Sample Code:
Dog d = new Dog();
Animal
makeNoise()
eat()
sleep()
roam()
Canine
roam()
Dog
makeNoise()
eat()
d.makeNoise();
uses makeNoise in Dog class
Cat
makeNoise()
eat()
d.roam();
uses roam in Canine class
d.eat();
uses eat in Dog class
d.sleep();
uses sleep in Animal class
Is-A Test
 In this Inheritance Tree, the
following are true:
- Canine is-A Animal
- Dog is-A Canine
- Dog is-A Animal
- Cat is-A Animal
 However, can't say Animal is-A
Dog as not all Animals are
dogs.
 A dog and cat object will have 4
total methods (of which they
override 2 of them)
L4 – L7 Summary continued…
Page 2
POLYMORPHISM
 The 2 basic concepts of polymorphism ("many forms") are:
1. A robot (or any object) does what it knows how to do when sent any message (ie: it is defined by its class).
2. Objects can behave differently to the same message (ie: move could mean 1 block or 1 mile)
CREATING OBJECTS
General Syntax: Class reference = new Class(….);
Example:
UrRobot karel
=
new UrRobot(1,1,North,0);
reference “karel”
ie: remote with buttons
"methods" to
move, turnLeft, etc.
YOU CAN CREATE YOUR OBJECTS IN TWO WAYS
Same class name on both sides.
1. Default
Ex: UrRobot karel = new UrRobot(…);
Parent class name on left side, child class name on right side
2. Polymorphic
Ex: BeeperLayer karel = new TwoRowLayer(…);
EXAMPLE OF POLYMORPHISM
Respond differently to same message
UrRobot karel = new BackwardsWalker(1,1,North,0);
karel.move(); moves 1 block backwards
karel = new MileWalker(1,1,North,0);
karel.move(); moves 1 mile
Notice the different types involved:
The reference (karel) is type UrRobot (a parent class)
The objects created are type BackwardsWalker, MileWalker.
The correct move method is picked based on what type the
object is and not the type of the reference.
ABSTRACT vs CONCRETE
 Concrete – you can define vs Abstract – too general to fully define
Abstract comes in play when you wish to create a parent class to share things with child classes. There can be
commands that are too general to define by the parent and are marked abstract and the children must define them.
Ex: BeeperLayer robots needed to lay beepers of different amounts so the command putBeepers was marked abstract.
 You can not instantiate an object using an abstract class
Ex: BeeperLayer karel = new BeeperLayer(…);
However, it is ok to make a reference using an abstract class. Ex: BeeperLayer karel = new TwoRowLayer(…);
INTERFACES
 Interfaces define what you can do (a contract) with objects (ie: methods) (like a user manual)
 An interface is like a 100% pure abstract class (ie: all methods must be overridden in class that uses interface)
 A class can extend only 1 class, but it can implement many interfaces
 In the class that implements the interface, you must define all abstract methods contained in the interface.
CLIENT/SERVER DESIGN
 server – a class/object providing a service (ie: a robot class like UrRobot, RooferBot)
 client – a main task block or another robot that is sending a message to server robot for service to be performed
Download