Abstract Classes and Interfaces. Let’s say we are working on a video game together… Our job is to independently write two different enemies for the game.

Slides:



Advertisements
Similar presentations
Java
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Abstract Class and Interface
Java Programming Abstract classes and Interfaces.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Computer Science 209 Software Development Equality and Comparisons.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Chapter 10 Classes Continued
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Abstract Classes and Interfaces
Abstract classes and Interfaces. Abstract classes.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
1 Lecture 07 Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p ProgramLive, Section 12.1 In User Interface.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
More on Polymorphism. Ever have one of those days?
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.,
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Coming up: Inheritance
Interfaces and Polymorphism CS 162 (Summer 2009).
When is a class not a class? When it is either abstract or an Interface.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Today’s lecture Review of chapter 8 Go over examples.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Inheritance and Polymorphism
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Module 9. Dealing with Generalization Course: Refactoring.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
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.
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Agenda Warmup AP Exam Review: Litvin A2
Interfaces Professor Evan Korth.
Interfaces and Inheritance
More on Classes & Arrays
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming in Java
Topics OOP Review Inheritance Review Abstract Classes
CIS 110: Introduction to computer programming
Presentation transcript:

Abstract Classes and Interfaces

Let’s say we are working on a video game together… Our job is to independently write two different enemies for the game. Skeleton draw(Graphics g) act(Map m, Player p) shoot(Player p) die() Zombie draw(Graphics g) act(Graphics g, Player p) shoot(int delta) dead() ArrayList enemies;

Let’s improve our design then… Enemy draw(Graphics g); die(); Skeleton draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); Zombie draw(Graphics g); die(); act(Map m); shoot(); ArrayList enemies; Enemies.get(i).draw(g); Enemies.get(i).die(); Enemies.get(i).act(m, p); ERROR!!!!

Why did we not define method act and method die in class Enemy? We hypothesized that subclasses would each implement these methods differently. We were right, but they implemented them too differently.

Let’s improve again… Abstract Enemy draw(Graphics g); die(); abstract act(Map m, Player p); abstract shoot(Player p); Skeleton draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); Zombie draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); ArrayList enemies; Enemies.get(i).draw(g); Enemies.get(i).die(); Enemies.get(i).act(m, p); Works Now!! No code for these methods!

Let’s take a different approach… interface Enemy draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); No code in this interface. Only method signatures. No fields allowed Skeleton implements Enemy draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); Zombie implements Enemy draw(Graphics g); die(); act(Map m, Player p); shoot(Player p); ArrayList enemies; Enemies.get(i).draw(g); Enemies.get(i).die(); Enemies.get(i).act(m, p);

sub-classes all share some behavior meaning we can add some methods (with code) that all sub- classes can inherit. sub-classes DO NOT share any behavior meaning we DO NOT want to add some methods (with code) that all sub-classes can inherit. Abstract classes interfaces

Every single class will implement comparable differently. Every object must compare against itself differently. Class Person Compare name and social security number Class Car Compare model, make, and VIN number. public class Person implement Comparable { /* Assume fields, contructors, methods */ public int compareTo(Object o) { Person other = (Person)o; if(name.equals(other.getName()) && ssn == other.getSsn()) return 0; //they are the same person else if(ssn < other.ssn()) return -1; //this is less than other else return 1; //this is greater than other }

You always want to return 0 if the two are equal (this and the other object being in). You always want to return a negative value if the calling object (this) is less than the argument. You always want to return a positive value if the calling object (this) is greater than the argument. The best solutions will return a positive or negative number that is an indication of how much less than or greater than (not just 1 or -1) the calling object is in comparison to the argument.

You can only extend one class resulting in a single super class. You may implement as many interfaces as you want!! Go crazy! Implemented interfaces are not super classes. For every interface you implement you must complete the methods specified by that interface.