Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized."— Presentation transcript:

1 CSC 205 Programming II Lecture 4 Abstract Class

2 The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized by concrete subtypes The abstraction might be too general to have practical usage May have constructors defined, although not meant to be used to create object May have one or more abstract methods

3 Abstract Method A class with one or more abstract methods must be abstract An abstract method doesn’t have a class body public abstract void draw(); Usually public. Define an common behavior yet each subtype might have a unique variation To be implemented by non-abstract subclasses

4 UML Representation Shape Rectangle +draw() Circle +draw() Triangle +draw()

5 Java Syntax public abstract class Shape { … public abstract void draw(Graphics g); //no class body … } public class Circle extends Shape { … public void draw(Graphics g) { g.setColor(getColor()); g.fillOval(getX(), getY(), diameter, diameter); } … }

6 Polymorphism Two definitions: Polymorphism is The facility by which a single operation may be defined upon more than one class and may take on different implementations in each of those classes Different shapes need to be draw in different ways The property where by a variable may point to objects of different classes at different times A variable in Shape type may refer to a Circle object at one time, and to a Rectangle object another time

7 Dynamic Binding The technique by which the exact piece of code to be executed is determined only at run-time AKA late or run-time binding run-time Shape s compile-time

8 Polymorphism – an example Shape Rectangle +toString() Square +toString() public String toString() { return "Edge: " + edge; } public String toString() { return "Position: (" + x + ", " + y + ")\t" + color; } public String toString() { return super.toString() + "\nWidth: " + width + "\tHeight: " + height; }

9 Late Binding – an example import java.awt.Color; public class ShapeTest { public static void main(String[] args) { Shape s1 = NervousShapes.createShape(); Shape s2 = new Rectangle(10, 10, Color.green, 15, 30); Shape s3 = new Square(50, 20, Color.red, 20); System.out.println(s1); System.out.println(s2); System.out.println(s3); } C:\courses\CSC205\labs\shape>java ShapeTest Position: (397, 106) java.awt.Color[r=13,g=91,b=241] Position: (10, 10) java.awt.Color[r=0,g=255,b=0] Width: 15 Height: 30 Edge: 20

10 Final Classes and Methods A final class cannot be extended Known final classes in Java APIs String Math Wrapper classes, such as Integer A class cannot be both abstract and final Methods declared as private or static are always final.


Download ppt "CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized."

Similar presentations


Ads by Google