1 / 27

Lecture 3

Lecture 3. Casting Abstract Classes and Methods Interfaces. Object Bicycle MountainBike RacingBike. Casting. 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 ?.

lise
Download Presentation

Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3 Casting Abstract Classes and Methods Interfaces

  2. Object Bicycle MountainBike RacingBike Casting 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 ?

  3. 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();

  4. Object Bicycle MountainBike RacingBike Casting and object types • 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** Cast

  5. You can check the type of an object.. if (yourBike instanceof MountainBike) { // Cast MountainBike newBike = (MountainBike)yourBike; }

  6. 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!

  7. Abstract Class • An abstract class represents a generic concepte.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

  8. 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

  9. 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)

  10. 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++

  11. Abstract Class Example See CODE • public abstract class Shape • { public abstract double area(); // note no body}

  12. 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... }}

  13. Implementing the abstract class • 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 • }

  14. Using the Shape Objects 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 (inti=0; i<shapes.length; i++){ totalArea+=shapes[i].area(); • 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

  15. Interfaces in java

  16. 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 fields double x; double y;} class CenteredCircle extends CenteredShape {…} etc…

  17. 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 interfacemyInterface { //method declarations ...}

  18. 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.

  19. 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

  20. 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();}

  21. 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{...}

  22. Implementing an Interface public class CenteredCircleextends Circle implementsCentered { // field defns double cx; //center x coord double cy; //center y coord // constructor public 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?

  23. Implementing an Interface // implementations for the interface methods public void setCenter(double x, double y){ cx=x; cy=y; } public double getCenterX(){ return cx; } public double getCenterY(){ return cy; }

  24. 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)

  25. 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

  26. 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

  27. 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.

More Related