CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 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 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 27 / 2006 Instructor: Michael Eckmann.
Object Oriented Software Development
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance in Java.
Types of Programming Languages
Designing for Inheritance
Advanced Java Topics Chapter 9
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
C++ Object Oriented 1.
Presentation transcript:

CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? protected vs. public vs. private

Protected vs. Private vs. Public subclasses have access to public and protected members of their superclass a class has access to all of its own members (whether they are private, protected or public)‏ objects of a class have access only to the public members of the class (and the public members of the parent class(es)). Classes within the same package though, are allowed to always access protected members --- but I don't recommend this.

Abstract classes – Can never be instantiated – Can contain both abstract methods and actual (non-abstract) methods – Can contain instance variables as well as constants If a class contains any abstract methods then it MUST BE an abstract class But an abstract class is not required to have abstract methods Abstract classes are different from interfaces which we will see next.

More on overriding methods Suppose we have a class Pet, Dog, and Cat. We provide a speak() method in Pet but with no body. Then, any class that inherits from Pet, must implement this method. Further, let's assume we have a class TalkingDog which doesn't bark when he speaks, instead he speaks English. To implement this stuff...

Class Pet, Dog and Cat Let's suppose all Pets have names, breeds and make sounds, and know how to sleep. Let's also suppose that different Dogs have different skills, but Cats do not. Data to be stored in Pet is: – Name – Breed Additional data to be stored in Dog is: – Skill No additional data is stored in Cat. Let's set up the classes with these data and the relationships among the classes.

More on overriding methods So we have classes Pet (abstract), Dog, and Cat. We can provide a speak() method in Pet with no body (and we make it abstract). Then, any class that inherits from Pet, must implement this method. We can provide an actual method sleeps() in Pet that is not abstract. And the class TalkingDog which doesn't bark when he speaks, instead he speaks English. So, Dog and Cat inherit from Pet. TalkingDog inherits from Dog. Let's implement this stuff (instantiate objects of Dog, Cat, and TalkingDog)‏ Note: because Pet contains an abstract method, the class is not instantiable --- the class itself must be declared as abstract.

abstract methods The reason we create an abstract method (with an empty method body) in the superclass is so that this guarantees that all subclasses must provide this method if they are to be instantiable. Make the method abstract if the superclass doesn't need to be instantiable and if there is no obvious default behavior for the method in the superclass --- this passes the requirement of the behavior of the abstract method to any subclass that wants to be instantiable. Notice: –a subclass of Pet had to include code for the speak() method –we couldn't instantiate an object of type Pet because it is abstract

Polymorphism Polymorphism --- having many forms A polymorphic reference variable is one that can refer to different types of objects at different points in time An example from a text I used in the past (Lewis and Loftus): obj.doIt(); If obj is polymorphic --- meaning it can take on the value of different types of objects throughout the running of the program then it might be calling different versions of the doIt() method. What does that mean --- different versions of the doIt() method?

Polymorphism It means doIt() may be a method that is in multiple classes and the actual method that gets executed depends on the type of the object. You can imagine that line of code: obj.doIt(); may be in a loop. Then, if the obj reference changes and actually refers to different types of objects, the different versions of doIt() are called. This deals with the concept of binding.

Binding Binding of a variable to a type usually occurs at compile-time. This means that when Java compiles the source code, it can figure out the exact type (class) associated with each variable name in the code at the time the code is compiled. In the case of polymorphism, this binding of a variable to a type can only be done at runtime. Why? This is called late binding or dynamic binding. Java needs to determine the ACTUAL type of the object being referred to by the reference. Pet p; // some code here to assign an object to p p.speak();

Polymorphism via Inheriance It can go futher. If Pet inherits from LivingThing, then this is allowed: LivingThing lt; Pet a_pet; Dog d = new Dog(“Max”); a_pet = d; lt = d; Here, the Dog reference is being stored in a reference (a_pet) of its immediate superclass (Pet) and also stored in a reference (lt) of a higher superclass (LivingThing.)‏

Polymorphism via Inheriance Below, some_pet is polymorphic because it can refer to a Dog object or a Cat object (or a Pet object if Pet is not abstract.)‏ Pet some_pet = new Dog(“Max”); System.out.println(some_pet.speak()); // Because there are subclasses of Pet (namely Dog // and Cat) the some_pet reference can refer to different // types of objects -> Java must know which speak()‏ // method to execute (the one in Dog, Cat or Pet)‏

Polymorphism via Inheriance Pet some_pet = new Dog(“Max”); some_pet = new Cat(“Kitty”); System.out.println(some_pet.speak()); That's where the dynamic binding of variable to actual type comes into play. Above, some_pet was a reference to a Dog then it became a reference to a Cat, so the speak() method is the Cat's speak method that it executes.

Polymorphism via Inheriance Let's add some code that stores an array of Pet references, but each object actually stored in the Pet references may be a Dog, Cat or TalkingDog object.

Overriding methods Polymorphism is possible via inheritance because of overriding methods. A subclass can override a superclass's method by providing a definition for a method that exists in the superclass with the same name and number and type of parameters. A class containing an abstract method is not instantiable (can't create objects of this class) instead it is used as a superclass. –If a subclass wants to be instantiable then the subclass that derives from the abstract superclass is required to implement (provide code for) any abstract methods in the superclass.

Overriding methods speak() was an abstract method in the parent class Pet. All subclasses of Pet were required to implement that method if the subclasses were to be instantiable. If speak() did not live in Pet, then we would still be allowed to have speak() methods in all the subclasses but we would lose a couple of benefits: –1) nothing requires the signatures of the speak() methods in each subclass to be the same. –2) we won't be able to call the speak() method using a reference to a Pet.

Polymorphism Being able to call the speak() method on a reference to a Pet --- that's the key concept here. Only during runtime, during the time that the actual method call occurs does Java know what kind of object is referenced by a Pet reference. A Pet reference could refer to a Dog, Cat or TalkingDog object (because these classes all derive from Pet directly or indirectly.)‏ Java then calls the appropriate speak() method based on which kind of object is actually calling speak() (i.e. a Dog, Cat or TalkingDog).