270 likes | 288 Views
Workshop for CS-AP Teachers. Chapter 3 Advanced Object-Oriented Concepts. Learning Goals. Understand at a conceptual and practical level Objects Inheritance Polymorphism. Object-Oriented Principles. Objects with data (fields) and operations (methods) Usually classes too Inheritance
E N D
Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts Georgia Institute of Technology
Learning Goals • Understand at a conceptual and practical level • Objects • Inheritance • Polymorphism Georgia Institute of Technology
Object-Oriented Principles • Objects with data (fields) and operations (methods) • Usually classes too • Inheritance • Hierarchy of types • Generalization / Specialization • Polymorphism • Executing the right method based on the type of the object Georgia Institute of Technology
Advantages to Objects • Data and related operations are combined • No passing data around • Data is protected • Objects are responsible • If code needs to be fixed you can figure out what class needs to be changed • Methods can change as long as they still do the job • Easier to maintain and extend • Objects don’t change as often as procedures • The program is a simulation of the domain • Classes can be reused Georgia Institute of Technology
Inheritance • Did you get features or abilities from your parents? • People inherit physical characteristics • Some people inherit abilities: music • Inheritance in OO means receiving data and methods from your parent • In Java you can only have one parent Georgia Institute of Technology
Inheritance in our Models • One class can inherit from another • Gets all the fields and methods • The class you inherit from is called • Parent, superclass, base class • The class doing the inheriting is called • Child, subclass, derived class Person Parent Student Child Georgia Institute of Technology
Inheritance Exercise • Open the Picture class (Picture.java) • See if you can find the show method • Notice that the Picture class extends (inherits) from SimplePicture • See if you can find the show method in SimplePicture • Since Picture inherits from SimplePicture you can ask a Picture object to show itself Georgia Institute of Technology
How Inheritance Works • When an object gets a message it checks to see if it has a corresponding method • If it does it executes that method • If it doesn’t it checks the parent class • And keeps looking until the method is found • The Java compiler makes sure that a method is available • based on the declared type of the object Georgia Institute of Technology
Teaching Inheritance • Point out inheritance in common objects • What is a book? • What is a dictionary? • Talk about the things that are the same • Talk about the things that are different Georgia Institute of Technology
The Object Class • If you don’t specify a parent for a class using “extends” it will inherit from Object • public class Name { • All objects in Java inherit from Object • Directly or indirectly • Object is in the package java.lang • All objects inherit • toString() and equals() but usually will override these Georgia Institute of Technology
Inheritance Exercise • Look up Object in the api • http://java.sun.com/j2se/1.4.2/docs/api/ • In package java.lang • What other public methods are in Object? • Look up String in the api • In package java.lang • What class does it inherit from? • Look up JButton in the api • In package javax.swing • What are all the classes it inherits from? Georgia Institute of Technology
Private Visibility and Inheritance • When a class inherits from another class it gets all the fields and methods • But, it can’t directly access fields or methods that are private • Use public methods to ask for changes to private fields • Call public methods that call private methods • This is to allow an object to protect its data and keep private methods private Georgia Institute of Technology
Protected and Inheritance • Some books use protected visibility with inheritance for fields • To give children objects direct access to fields • This is a bad idea • Protected gives access to subclasses • Protected also gives access to all classes in the same package • You can’t guarantee the data isn’t messed up by objects of other classes if you use protected fields! Georgia Institute of Technology
When to Use Inheritance • Use inheritance when one class is really a sub-type of another • You must be able to substitute an object of the child class for an object of the parent class and still have it make sense • Don’t use it just to have access to some fields and methods of the parent class • Use an association (has a) link instead Georgia Institute of Technology
Correct Use of Inheritance • Students and Teachers are people • You could use a student or teacher when you need a person • A course period is not a course • You wouldn’t include a course period in a list of available courses • Instead a course period would have a course associated with it Georgia Institute of Technology
Substitution • Is a dictionary a book? • If you need a book will a dictionary work? • You can use a child object when a variable refers to a parent object • SimplePicture p = new Picture(fileName); • You can’t substitute a parent for a child • Picture p = new SimplePicture(fileName) Georgia Institute of Technology
Generalization • Generalization means that classes have things in common • If they are several classes that all have the same or similar fields and methods • pull out the common items and put them in a parent class Georgia Institute of Technology
Generalization - Exercise • Pull out common fields and methods in Student and Teacher and put them in a new class Person • Have student and teacher inherit from Person • Run the main methods in Student and Teacher Georgia Institute of Technology
Advantages to Inheritance • Handles commonality • Common attributes and operations are factored out and put as high as possible in a hierarchy • Not copied to several locations • Handles differences • Children can inherit the parts that are common from the parent • They can add attributes and operations to handle how they differ from the parent • They can override operations (methods) to do something different from the parent Georgia Institute of Technology
Specialization • A class differs from the parent class in some way • It add fields • It adds methods • It does something different when given the same message as the parent Georgia Institute of Technology
Overriding Methods • One way a child class can specialize is to handle the same message in a different way than the parent • If you inherit parent methods how can you do this? • The child can redefine a method with the same name and parameters as the parent method • The new method is called instead of the parent method • This is called overriding Georgia Institute of Technology
How Does Overriding Work? • When a message is sent to an object • The object checks with its class to see if the corresponding method exists in the class • If it doesn’t exist in the class then it checks in the parent class • It keeps looking up the inheritance tree till it finds a corresponding method Georgia Institute of Technology
Using Super • What if the method in the child class wants to call the method in the parent class? • I want to do the same operation as my parent but also add something to it • But my method overrides the parent method • Use the keyword super to invoke an overridden parent method • It starts looking for a corresponding method in the parent class Georgia Institute of Technology
Overriding Exercise • Override the method toString in Student to print the name and grade point average • You can use super.toString to print the person information Georgia Institute of Technology
Polymorphism • Literally: many forms • In Object-Oriented development it means that what happens when a message is sent to an object depends on the type (class) of the object at runtime Georgia Institute of Technology
How Does Polymorphism Work? • If a class is declared to be final • then the compiler can figure out the location of a method that matches the message • If a class can be subclassed • then a variable declared to be of the parent type can point to an object of the parent class or any subclass at run-time • the compiler can’t determine the method to invoke • the method to invoke is figured out at run-time Georgia Institute of Technology
Advantages to Polymorphism • Used to create general algorithms that work on objects of different types • Collections that hold Objects • List, Set, Stack, Queue, Map • Makes it easy to add new types • Just create the new class and implement the required operations • Don’t change existing code Georgia Institute of Technology