Download presentation
Presentation is loading. Please wait.
Published byBarbara Thomasina Lloyd Modified over 9 years ago
1
Abstract Classes and Interfaces
2
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;
3
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!!!!
4
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.
5
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!
6
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);
7
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
8
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 }
9
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.
10
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.