Lecture 3 Casting Abstract Classes and Methods Interfaces.

27
Lecture 3 Casting Abstract Classes and Methods Interfaces

Transcript of Lecture 3 Casting Abstract Classes and Methods Interfaces.

Page 1: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Lecture 3

Casting

Abstract Classes and Methods

Interfaces

Page 2: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Casting

Object

Bicycle

MountainBikeRacingBike

A class hierarchy

Q: Mountainbike is a ? of bicycle?Q: what java keyword is used to do this hierarchy? Q:Does the Bicycle class use this keyword ?

Page 3: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Casting and object types

• When you create an object, its type is the class from which is was instantiated..e.g. ..

• myBike is a MountainBike (And it’s also is a Bicycle and an Object)– (Why an object?)

• But, a Bicycle may be a MountainBike but not necessarily…

– (could be a RacingBike)

MountainBike myBike = new MountainBike();

Page 4: Lecture 3 Casting Abstract Classes and Methods Interfaces.

• Casting allows the use of an object of one type in place of another type (down the hierarchy)

Bicycle yourBike = new MountainBike();

myBike = (MountainBike) yourBike; **OK**

CastObject

Bicycle

MountainBikeRacingBike

Casting and object types

Page 5: Lecture 3 Casting Abstract Classes and Methods Interfaces.

You can check the type of an object..

if (yourBike instanceof MountainBike)

{

// Cast

MountainBike newBike = (MountainBike)yourBike;

}

Page 6: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Casting

• Why are we looking at casting/object types?

• Because it’s used in GUI programming

• Used to check which GUI element was clicked etc… More later!

Page 7: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Class

• An abstract class represents a generic concept e.g. Shape, Food, Fruit, …

• it encapsulates the features that all kinds of elements of the concept have, e.g. Shapes have area, circumference

• these features are implemented as abstract methods e.g. area(), circumference()

• abstract methods cannot have an implementation meaningless at the conceptual, generic level

Page 8: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Classes

• An abstract class or method is defined by the keyword abstract– abstract class Shape {

…abstract double area();abstract double

cicumference();…

};• Any class with an abstract method is

automatically abstract and must be declared as such

Note: no body

Page 9: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Class

• An abstract class cannot be instantiated• A subclass of an abstract class can be

instantiated only if it overrides each of the abstract methods of its superclass and provides an implementation for all of them– this subclass is known as a concrete class

• If a subclass of an abstract class does not implement all the abstract methods it inherits, the subclass is itself abstract

• (Note: static, final and private methods cannot be abstract as they cannot be overridden)

Page 10: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Class

• An abstract class effectively defines a complete programming interface – providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface.– i.e. It defines what methods the subclass is going

to have to implement…

• Note: an abstract method in Java is like a pure virtual function in C++

Page 11: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Class Example

public abstract class Shape

{public abstract double area(); // note no body

}

See CODE

Page 12: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Implementing the abstract class __-__________________________________________-__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Public class Circle extends Shape

{

// put in attributes needed here...

public Circle (double radius)

{

// constructor code..

}public double getRadius()

{ // code...

}public double area()

{ //implement abstract method // Code for area calculation...}

}

Page 13: Lecture 3 Casting Abstract Classes and Methods Interfaces.

class Rectangle extends Shape{

// put in attributes needed here...

// constructor public Rectangle (double width,double height)

{// constructor code..

}

public double area()

{

//implement area abstract method...}// other methods ..etc

}

Implementing the abstract class

Page 14: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Using the Shape Objects

• Dynamic method lookup is used– the area of a circle is computed using the method

defined by Circle class – the area of a rectangle is computed using the

method defined by the Rectangle class

Shape[] shapes = new Shape[3]; //setup array of shapes

// then.. Instantiate the various shapes to be used..circle, square etc.

// then.. Calculate the total area of the shapes..double totalArea=0; // compute total area

for (int i=0; i<shapes.length; i++){totalArea+=shapes[i].area();

Page 15: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interfaces in java

Page 16: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interfaces

• Consider another type of shape, one where the centre point of the shape is known

• We want CenteredCircle to support area() and circumference() methods already defined without re-implementing the methods Problem, no multiple inheritance in java

abstract class CenteredShape {// instance fieldsdouble x;double y;

}class CenteredCircle extends CenteredShape {…} etc…

Page 17: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interfaces

• Solution an interface• An Interface is a simple a set of methods in

java – Use the type Interface instead of class in the java

source code…

public interface myInterface{

//method declarations...

}

Page 18: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interface

• An interface defines a protocol of behaviour that can be implemented by any class anywhere in the class hierarchy.

• It defines a set of methods but does not implement them.

• A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behaviour.

Page 19: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Some Interface Rules

• an interface contains no implementation.. Just the method names/signatures

• the methods of an interface are implicitly abstract (but abstract qualifier not needed)

• the only fields allowed in an interface are constants (i.e. declared static final)

• all methods of an interface are implicitly public

• an interface cannot be instantiated no constructor

Page 20: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interface Example

• The interface Centered is defined as

• It defines the methods that a Shape subclass should implement if it knows the x,y coords of its center point

public interface Centered {public void setCenter(double x,double y);public double getCenterX();public double getCenterY();

}

Page 21: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Implementing an Interface

• A class uses the keyword implements to implement one or more interfaces

public class className implements interfaceName {// class field definitions...// class method definitions...// interface method implementations...

}

public class class1 implements interface1,interface2{...}

Page 22: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Implementing an Interface

public class CenteredCircle extends Circle implements Centered {

// field defnsdouble cx; //center x coorddouble cy; //center y coord

// constructorpublic CenteredCircle(double cx, double cy,

double radius){super(radius);this.cx=cx;this.cy=cy;

}

Since the class CenteredCircle implements the circle interface, what extra methods must the CentredCircle class include?

Page 23: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Implementing an Interface

// implementations for the interface methodspublic void setCenter(double x, double y){

cx=x;cy=y;

}

public double getCenterX(){return cx;

}

public double getCenterY(){return cy;

}

Page 24: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Interfaces.. A bit more…

• Before writing a class definition, determine the public interface– the set of services offered to users of the class

• Java allows us to take this one stage further, by formally recording the interface as a Java interface

• a java interface is just a collection of abstract methods (i.e. we state the signatures, but not the bodies)

Page 25: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Using an Interface

• Where a class implements an interface instances of the class can be treated as objects of the interface type as well as objects of their own class type

• E.g. instances of CenteredCircle and CenteredRectangle can also be treated as

- instances of Shape as they extend Shape

- instances of Centered as they implement Centered

Page 26: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Quiz…what did you absorb?

• Explain in your own words what an interface is? Why is it needed in Java?

• Can an interface implement its methods? Can an abstract class?

• Can an interface be implemented by classes from different hierarchies? (e.g. a Bicycle class, and a Circle class?

• Can a class have more than one superclass?

• Can a class implement more than one interface?

Stopped here

Page 27: Lecture 3 Casting Abstract Classes and Methods Interfaces.

Abstract Classes vs Interfaces

• Differences between abstract classes and interfaces– An interface cannot have any implementation,

whereas an abstract class can. – An interface is not part of the class hierarchy.

Unrelated classes can implement the same interface.

– A class can implement many interfaces but can have only one superclass.