lecture33

lecture33 - COMP 250 Winter 2010 33 - abstract classes,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 250 Winter 2010 33 - abstract classes, casting April 9, 2010 Abstract classes It often occurs that one wants to deFne a class with some methods fully speciFed, but some methods speciFed only by their signature. Think of this as a hybrid between a full class and an interface. In Java, this hybrid is called an abstract class . One adds the modiFer abstract to the deFnition of the class and to each method that is missing its body. Here is an example: public abstract class Circular{ private double radius; Circular(){}; Circular(double radius){ this.radius = radius; }; public double getRadius(){ return radius; } public void setRadius(double radius){ this.radius = radius; } public abstract double getArea(); } This abstract class has one abstract method could be extended by concrete classes, e.g. Circle , Cylinder , Sphere which would each implement the method getArea() in a di±erent way. Note that the subclass Circle might also have a method getPerimeter() . Such a method would make no sense for a Sphere or Cylinder . Similarly, getVolume() would make sense for a Sphere and Cylinder , but not for a Circle . An abstract class cannot be instantiated. Despite this, abstract classes do have constructors. This seems like a contradiction, but it is not. Abstract classes are extended by concrete subclasses which provide the missing method bodies. When these subclasses are instantiated, they must inherit the Felds and methods of the superclass – in particular, the values of the Felds are set by the superclass constructor (either via an explicit super()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2011 for the course COMP 250 taught by Professor Blanchette during the Spring '08 term at McGill.

Page1 / 3

lecture33 - COMP 250 Winter 2010 33 - abstract classes,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online