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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
What is inheritance? It is the ability to create a new class from an existing class.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object-Oriented Design
Lecture 12 Inheritance.
Overriding Method.
03/10/14 Inheritance-2.
Testing Object-Oriented Software Concepts and Definitions
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Barb Ericson Georgia Institute of Technology May 2006
Week 6 Object-Oriented Programming (2): Polymorphism
Workshop for CS-AP Teachers
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Chapter 9 Carrano Chapter 10 Small Java
Workshop for Programming And Systems Management Teachers
More on Creating Classes part 3
Presentation transcript:

Georgia Institute of Technology 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 Student Parent 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 – –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