rit computer science dept. goals l inheritance l modifiers: private, public, protected l...

of 25 /25
RIT Computer Science Dept. Goals Inheritance Modifiers: private, public, protected Polymorphism

Author: hillary-blankenship

Post on 13-Jan-2016

217 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

Single Trial P3 Epoch Recognition in a Virtual EnvironmentQuestion from Review
Why do you have to cast an object (such as a String or Integer) being popped off of a stack and not an object (such as a String or Integer) being pushed onto the stack?
RIT Computer Science Dept.
A String is a type of Object
Is an Object a type of String?
No! This is why you have to tell the compiler what type of Object it’s dealing with
Note: You could ask the same about mammals and humans: A human is a type of mammal, but a mammal is not a type of human
RIT Computer Science Dept.
Inheritance
Inheritance is a relationship where one class shares the structure or behaviors defined in another class
RIT Computer Science Dept.
Why would you want
Code re-use
Provides a way to specify some properties/behaviors that all subclasses must exhibit
It’s an appropriate way to represent the “is-a-kind-of” relationship between classes
Inheritance also provides the ability to generalize -- a method can be written to work with the super-class but subclasses can be passed as arguments
RIT Computer Science Dept.
The Java Class Hierarchy
The root of the class hierarchy is the Object class
Every class (except the root) has exactly one parent
The parent is the (direct) superclass of the child
The children are (direct) subclasses of the parent.
RIT Computer Science Dept.
Questions
If class Tornado inherits from class Storm, mark the following statements as true or false:
There are more objects that conform to the type Tornado than there are that conform to the type Storm.
Tornado instances probably have more fields (attributes) than Storm instances, and definitely not fewer.
All methods declared in Storm can be called on instances of Tornado.
All methods declared in Storm will perform the same way whether they are invoked on instances of Storm or of Tornado.
RIT Computer Science Dept.
uses inheritance
Using the superclass Goose given in class, write a subclass for your favorite type of Goose and the noise that your favorite Goose makes.
RIT Computer Science Dept.
Which is the correct assignment?:
Goose myGoose = new GrannyGoose();
GrannyGoose myCat = new Goose();
RIT Computer Science Dept.
A subclass inherits everything from its superclass.
However, a subclass can override methods as shown in the GrannyGoose example [see handout]
A subclass can also add variables and methods
RIT Computer Science Dept.
Comments about super()
super() will call the constructor for the superclass of the class it is called in
It must be the first call in a constructor (or else Java gives a syntax error)
If you do not call super() yourself, Java will automatically add the call at the beginning of the constructor
It’s better to call super() yourself rather than to depend on the default behavior that Java currently has
RIT Computer Science Dept.
public TalkingGoose ( String name ) {
public void makeNoise ( ) {
Access
Most classes provide three levels of access to their members (state and behavior):
Public: can be accessed by the methods in a class, in its subclasses, and by other classes.
Protected: can only be accessed by methods in the class, in its subclasses, and in classes in the same package. (Note: Wu doesn’t include the info. about packages – see page 668)
Private: can only be accessed by methods in the class.
RIT Computer Science Dept.
Most classes are grouped into packages
For example, java.util is a package
Packages are like libraries of classes
If you don’t specify what package your class belongs to, it will belong to an unnamed default package
RIT Computer Science Dept.
Abstract Classes
An abstract class is a class from which no instances can be generated.
Abstract classes can contain abstract methods: methods without implementation
RIT Computer Science Dept.
Polymorphism
In its simplest form, polymorphism allows a variable of type X to refer to any object that is an instance of X or an instance of a subclass of X.
RIT Computer Science Dept.
Polymorphism (cont’d)
All classes are descendants of Object. So, a variable of type Object can refer to an instance of any class.
Using a Stack, this is why we can call push with a String argument, or with an Integer argument, or with a Rectangle argument, etc.
RIT Computer Science Dept.
Goose aGoose;
Object anotherGoose;
if ( Math.random() < 0.5 ) {
// Which method gets called depends on the type of
// object that aGoose refers to. This will be
// determined during run-time.
anotherGoose.makeNoise();
Goose aGoose = new WildCat( “Tigger” );
WolfGoose myGoose = aGoose;
WolfGoose myGoose = (WolfGoose) aGoose;
RIT Computer Science Dept.
Use inheritance to implement an “is-a” relationship
If A is a B, then make A a subclass of B
GrannyGoose is a type of Goose, so it is a subclass of Goose
Do not use inheritance to implement a “has-a” relationship
For example, a bird has feathers, a beak, toys, etc. These things should NOT be subclasses of Bird
This type of relationship is implemented by Bird containing an instance of Toy or Feathers and is called composition
RIT Computer Science Dept.
How Final Works on
Classes and Methods
A method may be declared as final. This means that the method cannot be overridden in a subclass.
A class may also be declared as final. Such a class cannot be subclassed.
RIT Computer Science Dept.