Lecture14

Lecture14 - Abstract Classes An abstract class is a special...

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

View Full Document Right Arrow Icon
1 Abstract Classes An abstract class is a special kind of class that cannot be instantiated. Abstract method methods do not have implementation To create an abstract method, just write the method declaration without the body and use the abstract keyword - No {} Example public abstract void doSomething();
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Abstract class is class which may contain one or more abstract methods, which is implemented by sub classes. An abstract class can contain abstract methods and also contain method with body. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. public abstract class Shape { private String color; public Shape(String myColor) { color = myColor;} public String getColor() { return color; } abstract int getArea(); } public class Rectangle extends Shape{ double ht = 0.0; double wd = 0.0; public double getArea() { return (ht*WD); } } public class Circle extends Shape { double r =0.0; public double getArea() { return (2 * 3.14 * r); } }
Background image of page 2
3 When to use abstract method and abstract class Abstract method are usually declared where two or more subclasses are expected to fulfill a similar role in different ways through different implementations (Polymorphism) these subclasses extends the same abstract class and provide different implementations for the abstract methods Sometimes, a class that you define represents an abstract concept that should not be instantiated. The Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a parent class to classes like Integer and Float, both of which implement specific kinds of numbers. A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class . An abstract class is a placeholder in a class hierarchy that represents a generic concept Use abstract classes to define broad types of behavior at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 More on Abstract Class We use the modifier abstract on the class header to declare a class as abstract. A class that’s not abstract is called concrete class. An abstract class must be extended; an abstract method must be overridden. public abstract class Shape { private String color; public Shape(String myColor) { color = myColor;} public String getColor() { return color; } abstract int getArea(); } public class Rectangle extends Shape{ double ht = 0.0; double wd = 0.0; public double getArea() { return (ht*WD); } } public class Circle extends Shape{ double r =0.0; public double getArea() { return (2 * 3.14 * r); } }
Background image of page 4
5 An abstract class can include concrete methods and instance variables. An abstract class does not need to include any abstract methods. The abstract
Background image of page 5

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

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

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 25

Lecture14 - Abstract Classes An abstract class is a special...

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

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