240 likes | 328 Views
Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java. Abstract Classes and Methods. Why Abstract Classes. Superclasses are created through the process called "generalization"
E N D
Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor
Programming with Java Abstract Classes and Methods
Why Abstract Classes • 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
Abstract Class Declared with abstract Can notbe 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
Notes 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
An Abstract Method is: • 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
Abstract Method 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
Abstract Class abstract class Shape { private intpositionX; private intpositionY; public void move (intnewX, intnewY) { positionX = newX; positionY = newY; } abstract draw(); // abstract resize(); // abstract abstract double getArea(); // methods abstract double getPerimeter(); // }
Concrete Class public class circle extends Shape{ private double radius; public void draw(){ . . . } public void resize(){ . . . } public double getArea(){ . . . } public double getPerimeter(){ . . . } }
Using Abstract Classes 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( )); } } }
Programming with Java Interfaces
Why Interfaces Polymorphism Multiple inheritance, sort of Class only extendsa single superclass but, implementsas many interfaces as it needs Interfaces can cross class hierarchies
An Interface 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
Interface Rules • 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
Basic Interface public class Something implements Doable { public void doThis(){ // whatever } public void doThat(){ // whatever } // etc. } public interface Doable { public static final String NAME; void doThis(); intdoThat(); void doThis2 (float value, char ch); booleandoTheOther (intnum); }
Multiple Inheritance 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(); 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() {…} }
Using Interfaces • You can write methods that work with more than one class • interface RuleSet { booleanisLegal(Move m, Board b); void makeMove(Move m);} • Every class that implements RuleSet must have these methods • class CheckersRules implements RuleSet { // one implementation public booleanisLegal(Move m, Board b) { ... } public void makeMove(Move m) { ... }} • This assignment is legal because a rulesOfThisGame object is a RuleSetobject • class ChessRules implements RuleSet { ... } // another implementationclass LinesOfActionRules implements RuleSet { ... } // and anotherRuleSetrulesOfThisGame = new ChessRules(); • This statement is legal because, whatever kind of RuleSet object rulesOfThisGame is, it must have isLegal and makeMovemethods • if (rulesOfThisGame.isLegal(m, b)) { makeMove(m); }
Abstract Classes Vs. Interfaces • 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
Class, Subclass, Abstract or Interface 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.
Summary 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
More Summary Extending only one class avoids the diamond of death Interface is like a 100% abstract class Create interfaces with interfaceinstead 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
Next Week • 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