1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS 221 - Computer Science II.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
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.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
1 Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 307 Fundamentals of Computer ScienceInterfaces and Abstract Classes 1 Topic 7 Interfaces and Abstract Classes “I prefer Agassiz in the abstract, rather.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Inheritance and Polymorphism
Lecture 6: Composition and Inheritance
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Object Oriented Programming (OOP) LAB # 8
Polymorphism.
More inheritance, Abstract Classes and Interfaces
null, true, and false are also reserved.
Extending Classes.
Java Programming Language
Polymorphism.
Week 6 Object-Oriented Programming (2): Polymorphism
CSE 143 Lecture 27: Advanced List Implementation
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
Advanced Programming in Java
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Java Inheritance.
Advanced Programming in Java
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Agenda Types and identifiers Practice Assignment Keywords in Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
CIS 110: Introduction to computer programming
Presentation transcript:

1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II

2 The Property Example  There are properties on a Monopoly board  Railroads, Utilities, and Streets are kinds of properties  One behavior we want in Property is the getRent method  problem: How do I get the rent of something that is “just a Property”?

CS Computer Science II 3 The Property class public class Property { private int cost; private String name; public int getRent() { return hmmmmm????? } Doesn’t seem like we have enough information to get the rent if all we know is it is a Property.

CS Computer Science II 4 Potential Solutions 1.Just leave it for the sub classes.  Have each sub class define getRent() 2.Define getRent() in Property and simply return -1.  Sub classes override the method with more meaningful behavior.

CS Computer Science II 5 Leave it to the Sub - Classes // no getRent() in Property public void printRents(Property[] props) { for(Property p : props) System.out.println(p.getRent()); } Property[] props= new Property[2]; props[0] = new Railroad("NP", 200, 1); props[1] = new Utility("Electric", 150, false); printRents(props); What is result of above code? A B. different every time C. Syntax errorD. Class Cast Exception E. Null Pointer Exception

CS Computer Science II 6 Fix by Casting // no getRent() in Property public void printRents(Property[] props) { for(Property p : props) { if(p instanceof Railroad) System.out.println( ((Railroad)).getRent() ); else if(p instanceof Utility) System.out.println( ((Utility)p).getRent() ); } Property[] props= new Property[2]; props[0] = new Railroad("NP", 200, 1); props[1] = new Utility("Electric", 150, false); printRents( props); What happens as we add more sub classes of Property ? What happens if one of the objects is just a Property ?

CS Computer Science II 7 Fix with Dummy Method // getRent() in Property returns -1 public void printRents(Property[] props) { for(Property p : props) System.out.println(p.getRent()); } Property[] props= new Property[2]; props[0] = new Railroad("NP", 200, 1); props[1] = new Utility("Electric", 150, false); printRents( props); What happens if sub classes don’t override getRent()? Is that a good answer?

CS Computer Science II 8 A Better Fix  We know we want to be able to find the rent of objects that are instances of Property  The problem is we don’t know how to do that if all we know is it a Property  Make getRent an abstract method  Java keyword

CS Computer Science II 9 Making getRent Abstract public class Property { private int cost; private String name; public abstract int getRent(); // I know I want it. // Just don’t know how, yet… } Methods that are declared abstract have no body an undefined behavior. All methods in a Java interface are abstract.

CS Computer Science II 10 Problems with Abstract Methods Given getRent() is now an abstract method what is wrong with the following code? Property s = new Property(); System.out.println(s.getRent());

CS Computer Science II 11 Undefined Behavior = Bad  Not good to have undefined behaviors  If a class has 1 or more abstract methods, the class must also be declared abstract. –version of Property shown would cause a compile error  Even if a class has zero abstract methods a programmer can still choose to make it abstract –if it models some abstract thing –is there anything that is just a “Mammal”?

CS Computer Science II 12 Abstract Classes public abstract class Property { private int cost; private String name; public abstract double getRent(); // I know I want it. // Just don’t know how, yet… } // Other methods not shown If a class is abstract the compiler will not allow constructors of that class to be called Property s = new Property(1, 2); //syntax error

CS Computer Science II 13 Abstract Classes  In other words, you can’t create instances of objects where the lowest or most specific class type is an abstract class  Prevents having an object with an undefined behavior  Why would you still want to have constructors in an abstract class?  Object variables of classes that are abstract types may still be declared Property s; //okay

CS Computer Science II 14 Sub Classes of Abstract Classes  Classes that extend an abstract class must provided a working version of any abstract methods from the parent class –or they must be declared to be abstract as well –could still decide to keep a class abstract regardless of status of abstract methods

CS Computer Science II 15 Implementing getRent() public class Railroad extends Property { private static int[] rents = {25, 50, 10, 200}; private int numOtherRailroadsOwned;; public double getRent() { return rents[numOtherRailroadsOwned];} // other methods not shown }

CS Computer Science II 16 A Utility Class

CS Computer Science II 17 Polymorphism in Action // getRent() in Property is abstract public void printRents(Property[] props) { for(Property p : props) System.out.println(p.getRent()); } Add the Street class. What needs to change in printRents method? Inheritance is can be described as new code using old code. Polymorphism can be described as old code using new code.

CS Computer Science II 18 Comparable in Property public abstract class Property implements Comparable { private int cost; private String name; public abstract int getRent(); public int compareTo(Property other) { return this.getRent() – otherProperty.getRent(); }

Back to Lists  We suggested having a list interface public interface IList extends Iterable { public void add(E value); public int size(); public E get(int location); public E remove(int location); public boolean contains(E value); public void addAll(List other); public boolean containsAll(List other); } CS Computer Science II 19

Data Structures When implementing data structures: -Specify an interface -Create an abstract class that is skeletal implementation interface -Create classes that extend the skeletal interface CS Computer Science II 20