CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
Abstract Classes and Interfaces
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Abstract classes and Interfaces. Abstract classes.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Polymorphism.
More about inheritance
Lecture 9-2: Interacting with the Superclass (super);
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Interfaces and Inheritance
Lecture 19 - Inheritance (Contd).
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Java Inheritance.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Object Oriented Programming
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
F II 8. More on Inheritance Objectives
Presentation transcript:

CPSC150 Abstract Classes Chapter 10

CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void print( ) { } Sample Entry: Student Susie Smith 123 Main Street York River East (757) Student Home Address School Address public void print ( ) { } subclass superclass subclass superclass

CPSC150 DirectoryEntry print (note: your assignment does not have a print method) public class DirectoryEntry { private String name; private String phone; public void print( ) { System.out.println( “Name: “ + name + “\nPhone: “ + phone); }

CPSC150 Student print (note: your assignment does not have a print method) public class Student extends DirectoryEntry { private String homeAddress; private String schoolAddress; public void print( ) { System.out.println(“Student: “ + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); }

CPSC150 will call subclass print only public class Directory { ArrayList dir; // methods to create and fill dir public void print( ) { for (int i=0;i<dir.size();i++) { DirectoryEntry de = dir.get(i); de.print( ); } Directory (note: your Directory is database, not printed) Student Home: 123 Main Street School: 23 York River East no name!

CPSC150 which print DirectoryEntry objects will call DirectoryEntry print Student objects will call Student print Student print overrides DirectoryEntry print both Student and DirectoryEntry types are in ArrayList which prints depends on what type the element is. determined at runtime

CPSC150 Polymorphism For DirectoryEntry, print method prints name and phone For Student, print method prints home address and school address Which is printed is determined at compile time wikipedia: "polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). "objectstypes methods dynamic binding

CPSC150 Problem Call both Student AND DirectoryEntry to get print from both

CPSC150 Solution: fix Student print private String homeAddress; private String schoolAddress; public void print( ) { System.out.println( “Student: “ + name + “\nPhone: “ + phone + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); } private access in parent; can’t use

CPSC150 Solution Try #1 call parent then child public void print( ) { super.print( ); System.out.println(“Student: “ + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); } –Problem now: print doesn’t look good Susie Smith Phone: (757) Student: Home: 123 Main Street School: 23 York River East

CPSC150 Solution #2 use protected protected String name; protected String phone –allows child access, but not others –solves the problem, but frowned on use accessor methods –works, but allows access that may not be desired use protected accessor methods

CPSC150 Solution: Protected access methods ! public class DirectoryEntry { private String name; private String phone; protected String getName( ) { return name; } protected String getPhone( ) {return phone; } public class Student extends DirectoryEntry { //private instance fields public void print( ) { System.out.println( “Student: “ + super.getName( ) + “\nPhone: “ + super.getPhone( ) + “\nHome: “ + homeAddress + “\nSchool: “ + schoolAddress); }

CPSC150 Now, DirectoryEntry print isn’t needed remove it (code not shown), but now: public class Directory { ArrayList dir; // methods to create and fill dir public void print( ) { for (int i=0;i<dir.size();i++) { DirectoryEntry de = dir.get(i); de.print( ); } // will cause syntax error if // DirectoryEntry has no print method }

CPSC150 Yet another problem Need print method for syntax checker Don’t need print method for runtime Solution: Leave it in there. Problem: Bad design

CPSC150 Final Solution: Abstract Methods Make print method abstract –means it cannot be called/used Leaves it there for compiler Shows that it will not be used for design

CPSC150 Abstract Classes If a method is abstract, it cannot be called So, an object of that class can’t exist So any class that has an abstract method MUST be abstract Abstract classes can have any mix of concrete and abstract methods Concrete classes can be called by children objects of the class

CPSC150 Syntax of abstract methods abstract public class MyClass { // regular constructors and methods that are called in a regular way abstract method signature ; //no body. NO {}s }

CPSC150 Why Abstract Methods Abstract Methods require any children to implement that method Compile time error if abstract method not in subclass Allow clients that use the code to compile with the guarantee that that method will be implemented

CPSC150 Your turn Write the abstract class DirectoryEntry. Make the print method abstract Note: you will not have a print method for your assignment

CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class SubClass1 extends SuperClass first line of constructo r is super( ); can use methods from SubClass1 or SuperClass private in parent is not accessible to children; protected is Subclasses can have only one parent subclass can override methods in parent. overriden methods can be accessed by super.method( ); which method is called is determined by polymorphis m at runtime

CPSC150 Inheritance Review Two reasons for inheritance 1.To use the methods of the parent 2.To use polymorphism (e.g. add DirectoryEntry to a directory, but each entry is a student, faculty or staff; directory doesn’t have to know which)

CPSC150 Abstract Classes Review Abstract methods enable polymorphism, but have no body Classes with abstract methods must be abstract Abstract classes can not have objects created from them Abstract classes can have useful concrete methods (e.g., getName) and fields (e.g.,name, phone)

CPSC150 Abstract Class Review Two reasons for Abstract Classes 1.Enables polymorphism when methods are not appropriate for superclass (e.g., draw in Shapes or print in DirectoryEntry) 2.Enforces a specification (in order to be a DirectoryEntry, you must have a print method)