DM550 / DM857 Introduction to Programming Peter Schneider-Kamp - - PowerPoint PPT Presentation

dm550 dm857 introduction to programming peter schneider
SMART_READER_LITE
LIVE PREVIEW

DM550 / DM857 Introduction to Programming Peter Schneider-Kamp - - PowerPoint PPT Presentation

DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/DM550/ http://imada.sdu.dk/~petersk/DM857/ ADVANCED OBJECT -ORIENTATION 2 June 2009 Object-Oriented Design classes often


slide-1
SLIDE 1

DM550 / DM857 Introduction to Programming Peter Schneider-Kamp

petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/DM550/ http://imada.sdu.dk/~petersk/DM857/

slide-2
SLIDE 2

ADVANCED OBJECT

  • ORIENTATION

June 2009 2

slide-3
SLIDE 3

Object-Oriented Design

§ classes often do not exist in isolation from each other § a vehicle database might have classes for cars and trucks § in such situation, having a common superclass useful § Example: public class Vehicle { public String model; public int year; public Vehicle(String model, int year) { this.model = model; this.year = year; } public String toString() {return this.model+" from "+this.year;} }

June 2009 3

slide-4
SLIDE 4

Extending Classes

§ Car and Truck then extend the Vehicle class § Example: public class Car extends Vehicle { public String colour; public Car(string model, int year, String colour) { this.colour = colour; // this makes NO SENSE } public String toString() { return this.colour; } } public class Truck extends Vehicle { public double maxLoad; … }

June 2009 4

slide-5
SLIDE 5

Class Hierarchy

§ class hierarchies are parts of class diagrams § for our example we have:

June 2009 5

Vehicle Car is-a Truck is-a Object is-a

slide-6
SLIDE 6

Abstract Classes

§ often, superclasses should not have instances § in our example, we want no objects of class Vehicle § can be achieved by declaring the class to be abstract § Example: public abstract class Vehicle { public String model; public int year; public Vehicle(String model, int year) { this.model = model; this.year = year; } public String toString() {return this.model+" from "+this.year;} }

June 2009 6

slide-7
SLIDE 7

Accessing Attributes

§ attributes of superclasses can be accessed using “this” § Example: public class Car extends Vehicle { public String colour; public Car(string model, int year, String colour) { this.model = model; this.year = year; this.colour = colour; } public String toString() { return this.colour+" "+this.model+" from "+this.year; } }

June 2009 7

slide-8
SLIDE 8

Accessing Superclass

§ methods of superclasses can be accessed using “super” § Example: public class Car extends Vehicle { public String colour; public Car(String model, int year, String colour) { this.model = model; this.year = year; this.colour = colour; } public String toString() { return this.colour+" "+super.toString(); } }

June 2009 8

slide-9
SLIDE 9

Superclass Constructors

§ constructors of superclasses can be accessed using “super” § Example: public class Car extends Vehicle { public String colour; public Car(string model, int year, String colour) { super(model, year); this.colour = colour; } public String toString() { return this.colour+" "+super.toString(); } }

June 2009 9

slide-10
SLIDE 10

Abstract Methods

§ abstract method = method declared but not implemented § useful in abstract classes (and later interfaces) § Example: public abstract class Vehicle { public String model; public int year; public Vehicle(string model, int year) { this.model = model; this.year = year; } public String toString() {return this.model+" from "+this.year;} public abstract double computeResaleValue(); }

June 2009 10

slide-11
SLIDE 11

Implementing Abstract Methods

§ abstract methods need to be implemented in concrete subclasses § use same function signature, but without “abstract” § Example: public class Car extends Vehicle { ... public double computeResaleValue() { double value = 100000 * (this.model.startsWith("Audi") ? 6 : 4); value *= (this.year-2000)/20; return value; } }

June 2009 11

slide-12
SLIDE 12

Interfaces

§ different superclasses could have different implementations § to avoid conflicts, classes can only extend one (abstract) class § interfaces = abstract classes without implementation § only contain public abstract methods (abstract left out) § no conflict possible with different interfaces § Example: public interface HasValueAddedTax { public double getValueAddedTax(double percentage); } public class Car implements HasValueAddedTax { public double getValueAddedTax(double p) { return 42000; } … }

June 2009 12

slide-13
SLIDE 13

Interfaces

§ Example: public interface HasValueAddedTax { public double getValueAddedTax(double percentage); } public interface Destructible { public void destroy(); } public class Car implements HasValueAddedTax, Destructible { public double getValueAddedTax(double p) { return 42000; } public void destroy() { this.model = "BROKEN"; } … }

June 2009 13

slide-14
SLIDE 14

Interface and Class Hierarchy

§ interfaces outside normal class hierarchy

June 2009 14

Vehicle Car Truck

HasValueAddedTax Destructible

slide-15
SLIDE 15

Inner Classes

§ classes and interfaces can be nested § inner class = class contained in another class § Example: public abstract class Vehicle { … public interface Destructible { public void destroy(); } public class Car extends Vehicle implements Destructible { … } }

June 2009 15

slide-16
SLIDE 16

Local Classes

§ classes and interfaces can be declared in function bodies § local class = class contained in the body of a function or method § Can obviously not be public § Example: public static void main(String[] args) { class Bicycle implements Destructible { public void destroy() { System.out.println("Ouch!"); } } new Bicycle().destroy(); }

June 2009 16

slide-17
SLIDE 17

Anonymous (Sub-)Classes

§ possible to create anonymous classes § often used to instantiate abstract classes or interfaces § body of class defined after constructor call § Example: public class FarmVillain { public static void main(String[] args) { Vehicle x = new Vehicle("Volvo T230",1971) { public double computeResaleValue() { return 25000; } }; } }

June 2009 17

slide-18
SLIDE 18

Final Modifier

§ variables only assigned once can be declared final § multiple assignment to final variable results in compiler error § Example: final int x; x = 42; // ok x = 23; // ERROR

June 2009 18

slide-19
SLIDE 19

Local and Anonymous Classes

§ local and anonymous classes can access local variables and parameters IF they are final § Example: public static makeTractor(String model, int year, final int base) { final double factor = (year-1920)/100; return new Vehicle(model,year) { public int computeResaleValue() { return base*factor; } }; }

June 2009 19