Presentation is loading. Please wait.

Presentation is loading. Please wait.

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.

Similar presentations


Presentation on theme: "Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts."— Presentation transcript:

1 Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts

2 Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level –Objects –Inheritance –Polymorphism

3 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

4 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

5 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

6 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 Student Parent Child

7 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

8 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

9 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

10 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

11 Georgia Institute of Technology Inheritance Exercise Look up Object in the api –http://java.sun.com/j2se/1.4.2/docs/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?

12 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

13 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!

14 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

15 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

16 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)

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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


Download ppt "Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts."

Similar presentations


Ads by Google