Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Similar presentations


Presentation on theme: "COP 2800 Lake Sumter State College Mark Wilson, Instructor."— Presentation transcript:

1 COP 2800 Lake Sumter State College Mark Wilson, Instructor

2 Abstract Classes and Methods

3  Superclasses are created through the process called "generalization" Common features (methods or variables) are factored out of object classifications (ie. classes). Those features are formalized in a class. This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class  Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world It is an artifact of the generalization process  Because of this, abstract classes cannot be instantiated  They act as place holders for abstraction

4  Declared with abstract  Can not be instantiated  No constructor  Can be subclassed  Can contain concrete and abstract methods  If a class includes abstract methods, then the class itself must be declared abstract  Cannot be declared as final

5  An abstract method cannot be contained in a non-abstract class  If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract.  A non-abstract subclass extended from an abstract class must implement all the abstract methods, even if they are not used in the subclass  A subclass can be abstract even if its superclass is concrete

6  Has a signature but no implementation  Is a placeholder. Method must exist No meaningful implementation within this class  Any class which contains an abstract method MUST also be abstract  Any class which has an incomplete method definition cannot be instantiated (ie. it is abstract)  If a method can be implemented within an abstract class, and implementation should be provided

7  Declared with abstract  No implementation  No braces, semicolon after the parameter list  Must be implemented (overridden) by a subclass  An abstract method cannot be defined as final or static

8

9 abstract class Shape { private int positionX; private int positionY; public void move (int newX, int newY) { positionX = newX; positionY = newY; } abstract draw(); // abstract resize(); // abstract abstract double getArea(); // methods abstract double getPerimeter(); // }

10 public class circle extends Shape{ private double radius; public void draw(){... } public void resize(){... } public double getArea(){... } public double getPerimeter(){... }

11 public class ShapeShifter { public static void main (String [] args) { Shape[] shapeList = { new Circle(3.0), new Rectangle(3.0, 4.0), new Rectangle(2.5, 7.5), new Circle(2.5), new Square(5.0) } for (int i = 0; i < shapeList.length; i++) { System.out.print (shapeList[i].toString( ) + “ ”); System.out.print (shapeList[i].area( ) + “ ”); System.out.println (shapeList[i].perimeter( )); }

12 Interfaces

13  Polymorphism  Multiple inheritance, sort of  Class only extends a single superclass but, implements as many interfaces as it needs  Interfaces can cross class hierarchies

14  Special case of abstract class  Declares abstract methods therefore no implementations  Can define constants ( final )  Defines a usage contract between classes  By definition public  By definition abstract

15  A class can implement multiple interfaces: the interfaces are listed in the implements clause, separated by commas  A class that implements an interface, must define all methods in the interface  A class that implements an interface can implement other methods as well

16 public class Something implements Doable { public void doThis(){ // whatever } public void doThat(){ // whatever } // etc. } public interface Doable { public static final String NAME; void doThis(); int doThat(); void doThis2 (float value, char ch); boolean doTheOther (int num); }

17

18 public interface Speaker { public void speak(); } class Philosopher extends Human implements Speaker { public void speak() {…} public void pontificate() {…} } class Dog extends Animal implements Speaker { public void speak() {…} } Speaker guest; guest = new Philosopher(); guest.speak(); guest = Dog(); guest.speak(); Speaker special; special = new Philosopher(); special.pontificate(); // compiler error Speaker special; special = new Philosopher(); ((Philosopher)special).pontificate();

19  You can write methods that work with more than one class interface RuleSet { boolean isLegal(Move m, Board b); void makeMove(Move m); }  Every class that implements RuleSet must have these methods class CheckersRules implements RuleSet { // one implementation public boolean isLegal(Move m, Board b) {... } public void makeMove(Move m) {... } }  This assignment is legal because a rulesOfThisGame object is a RuleSet object class ChessRules implements RuleSet {... } // another implementation class LinesOfActionRules implements RuleSet {... } // and another RuleSet rulesOfThisGame = new ChessRules();  This statement is legal because, whatever kind of RuleSet object rulesOfThisGame is, it must have isLegal and makeMove methods if (rulesOfThisGame.isLegal(m, b)) { makeMove(m); }

20  Abstract Classes: Can have data fields Methods may have an implementation Classes and abstract classes extend abstract classes. Class cannot extend multiple abstract classes Substitution principle is assumed  Interfaces: Can only have constants Methods have no implementation Classes and abstract classes implement interfaces Interfaces can extend multiple interfaces A class can implement multiple interfaces Substitution principle not assumed

21  Make a class that doesn’t extend anything when your class doesn’t pass the IS-A test for any other type  Extend a class only when you need to make a more specific version of a class AND need to override or add new behaviors  Use an abstract class to define a template for a group of classes or when you want to guarantee nobody makes objects of that type  Use an interface to define a role other classes can play regardless of where the classes are in the inheritance tree.

22  Abstract classes can’t be instantiated  Abstract class can have concrete and abstract methods  A class must be abstract if it has any abstract methods  Abstract methods has no implementation and the declaration ends with a semicolon  All abstract methods must be implemented in the first concrete subclass in the inheritance tree

23  Extending only one class avoids the diamond of death  Interface is like a 100% abstract class  Create interfaces with interface instead of class  Implement interfaces with implements keyword  Classes can implement multiple interfaces  Classes that implement interfaces must implement all the methods of the interface  All interface methods are implicitly public and abstract  Interfaces can apply to any class regardless of the class heirarchy

24  Review of concepts, syntax and terminology  Mid-term exam ~ 100 objective style questions T-F Multiple guess Fill in the blank Short answer, including code fragments Matching  Anything covered in the reading assignments or the lectures is fair game


Download ppt "COP 2800 Lake Sumter State College Mark Wilson, Instructor."

Similar presentations


Ads by Google