1 2 1
is a mechanism for enhancing existing classes. It allows to extend the description of an existing class by adding new attributes and new methods. For example: class ColoredRectangle extends Rectangle { //here comes the class body of ColoredRectangle Rectangle is called superclass and ColoredRectangle subclass. One important reason: code reuse. 3 Example: import java.awt.color; class Rectangle { private int x, y, height, width; public void draw() { public void move(int dx, int dy){ class ColoredRectangle extends Rectangle { private Color FillColor; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void move(int dx, int dy){ 4 2
The subclass inherits all the methods of its superclass. For example, the following methods calls are possible: ColoredRectangle r = new ColoredRectangle(); r.left(); r.move(10.0, 20.0); Additionally, in the subclass new methods can defined and used by subclass-objects. r.setfillcolor(color.gray); Additionally, in the subclass methods of the superclass canbeoverwritten. Attributes of the subclass, that are instance variables, can be newly defined or they can be inherited of the superclass (but not overwritten). 5 import java.awt.color; class ColoredRectangle extends Rectangle { private Color FillColor; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void draw() { //draws the rectangle and fills it with color 6 3
import java.awt.color; class ColoredRectangle extends Rectangle { private Color FillColor; public ColoredRectangle(Color c){ //initializes a new colored rectangle super(); //that must be first statement of the subclass constructor FillColor = c; public Color getfillcolor() { return this.fillcolor; public void setfillcolor(color c) { this.fillcolor = c; public void draw() { //draws the rectangle and fills it with color super.draw(); 7 Examples: (in chapter 13) SavingsAccount (Sparkonto) CheckingAccount (Girokonto) 8 4
9 Generalization Spezilization 10 5
Common Error Shadowing Instance Fields, for example 11 - IsRelation Polymorphism: Rectangle Rectangle r; ColoredRectangle c; r = new ColoredRectangle(Color.blue); //o.k. c = r; c = (ColoredRectangle)r; Coloured Rectangle //error //o.k. 12 6
Handling and Late Binding: Rectangle rect[]; rect = new Rectangle[100]; //Objekte werden den Referenzen zugewiesen rect[2]= new ColoredRectangle(); for (i=0; i<rect.length; i++) { rect[i].move(dx,dy); rect[i].draw(); 13 Abstract methods and abstract classes: An abstract class is class from which no objects can be created. An abstract method is a method without any body, that is with no implementation. abstract public void draw(); A class which has an abstract method has to be declared abstract. A class without abstract methods can also be declared abstract. Why abstract classes? 14 7
Example: GraphicObject is a superclass for objects which can be drawn and moved on a panel. The draw method can not be implemented, since it is dependent from the concrete subclass objects: It has to be declared abstract. abstract public class GraphikObject{ double x, y; public void move(double dx, double dy){ x+=dx; y+=dy; abstract public void draw(); class Rectangle extends GraphikObject { public void draw() { //owverwrite and implements the abstract method //of the superclass GraphikObject 15 Interfaces revisited: Remember our class ColoredRectangle: class ColoredRectangle extends Rectangle{ What to do if this class should also be a subclass of GeometricObject? Perhaps: class ColoredRectangle() extends Rectangle, GeometricObject { //error Multiple inheritance is not possible in Java! Use an interface: interface GeometricObject { class ColoredRectangle() extends Rectangle implements GeometricObject { //o.k. 16 8
Key word: final This key word can also be used in class definition public final class String { That means that class String can not be extended (by a subclass). There are two reasons: 1. The compiler can generate more efficient method calls (no late binding) 2. Also the String class is meant to be immutable: string objects can not be modified by their own methods. All String references can be copied without the risk of mutation. 17 Key word: final This key word can also be used in method definition public class SecureAccount extends BankAccount { public final boolean checkpassword(string ){ That means that it is not possible to override this method, that is a method with the same signature, by a method in a subclass. 18 9
Object: The cosmic superclass. Object is superclass of every other class. It contains the methods: String tostring(): Returns a string representation of the object. Used in Rectangle cerealbox = new Rectangle(5, 10, 20, 30); cerealbox= + cerealbox; boolean equals(object otherobject): Tests whether the object equals another object. Object clone(): Makes a full copy of an object. 19 20 10
equals-method The test (coin1 == coin2) is not sufficient, since it is checked whether two references are equal 21 But: We want to know whether: Object 1 and object 2 have the same state. Use the method: boolean equals(object) Make a previous check with getclass()! 22 11
If the clone()-method of the class Object is used. 23 A deep copy is gained by recursively calling the clone()-method for all referenced objetcs. As a result, the account-object in our example is also cloned. Note, the Cloneable-interface has to be implemented, if clone() is implemented, and the clone()-method of Object can throw exceptions. 24 12