10-20101108 - Introduction to OO Program Design Software...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to OO Program Design Software College of SCU Instructor: Shu Li Email: [email protected] 2 Unit 2.3 Advanced Class Design • 2.3.1 Abstract Classes ( o ) • 2.3.2 Polymorphism ( o ) • 2.3.3 Interfaces( o ) 3 Abstract Method • Definition: – Methods that do not have implementation (body) • To create an abstract method, just write the method declaration without the body and use the abstract keyword – No { } • For example, // Note that there is no body public abstract void eat(int amount); public abstract void sleep(int hours); 4 Abstract Class. • Definition: An abstract class is a class that contains one or more abstract methods – Any class which contains an abstract method must itself be declared abstract. • An abstract class is denoted by using the abstract modifier – public abstract class ClassName { . . . } 5 Abstract Class.. • An abstract class cannot instantiated – No instance of an abstract class can be created. // You will get a compile error on the following code MyAbstractClass a1 = new MyAbstractClass(); • If a subclass of an abstract class does not implement all abstract methods inherited from its parent, the subclass must also be defined as abstract. 6 Example • Consider a chemical company that transports its products in two kinds of containers: wagons and tanks: 7 Class Diagram +computeVolume() : double Container +computeVolume() : double-width : double-height : double-length : double Wagon +computeVolume() : double-radius : double-length : double Tank 8 Class Container public abstract class Container { /** * Computes the volume of this container. * @return the volume of this container. */ public abstract double computeVolume(); } 9 Class Wagon public class Wagon extends Container { private double width; private double height; private double length; public Wagon(double initialWidth, double initialHeight, double initialLength) { width = initialWidth; height = initialHeight; length = initialLength; } public double computeVolume() { return width * height * length; } } 10 Class Tank public class Tank extends Container { private double radius; private double length; public Tank(double initialRadius, double initialLength) { radius = initialRadius; length = initialLength; } public double computeVolume() { return Math.PI * radius * radius * length; } } 11 Business System +getName() : String-name : String Person +getAge() : int-age : int Employee +getCredit() : double-credit : double Client 12 Class Person public abstract class Person { private String name; public Person(String initialName) { name = initialName; } public String getName() { return name; } } Class can be defined as abstract but it does not contain any abstract methods. 13 Class Employee public class Employee extends Person { private int age; public Employee(String initialName, int initialAge) { super(initialName); age = initialAge; } public int getAge() { return age; } } 14 Class Client public class Client extends Person { private double...
View Full Document

{[ snackBarMessage ]}

Page1 / 62

10-20101108 - Introduction to OO Program Design Software...

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

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