Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Abstract Classes and Interfaces Part 01

Similar presentations


Presentation on theme: "Chapter 13 Abstract Classes and Interfaces Part 01"— Presentation transcript:

1 Chapter 13 Abstract Classes and Interfaces Part 01

2 Abstract Classes & Abstract Methods
An abstract class is a class that is declared abstract Abstract classes cannot be instantiated, but they can be subclassed Abstract methods are declared when their implementations are dependent on the subclasses where they are invoked, e.g. getArea() in GeometricObject Abstract methods are non-static methods that are declared without implementations (without {}, but followed by a ;): public abstract class Account { // declare data fields // declare concrete methods abstract void check(double balance); } 2

3 Why do we use Abstract Classes ?
The purpose of an abstract class is to function as a base for subclasses. Inheritance child can have only one base abstract class

4 Abstract Classes & Abstract Methods
GeometricObject Circle Rectangle TestGeometricObject 4 Run

5 Abstract Classes & Abstract Methods
In a concrete subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract An abstract class may contain just concrete methods, or just abstract methods, or both or none! A concrete class cannot contain abstract methods 5

6 Abstract Classes A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract We cannot create an instance from an abstract class using the new operator, but: We can include constructors (to be used by subclasses) in an abstract class An abstract class can be used as a data type. For example, the following statement, which creates an array whose elements are of abstract class GeometricObject type, is correct GeometricObject[] geo = new GeometricObject[10]; 6

7 Case Study: Abstract Number Class
LargestNumbers Run 7

8 Source: https://docs. oracle. com/javase/7/docs/api/index. html

9 Abstract Calendar Class & its Concrete GregorianCalendar Subclass
9

10 Source: https://docs. oracle. com/javase/7/docs/api/index. html

11 Abstract Calendar Class & its Concrete GregorianCalendar Subclass
An instance of java.util.Date represents a specific instant in time with millisecond precision java.util.Calendar is an abstract base class for extracting detailed information such as year, month, date, hour, minute and second from a Date object Subclasses of Calendar can implement specific calendar systems such as Gregorian calendar, Lunar Calendar and Jewish calendar Currently, java.util.GregorianCalendar for the Gregorian calendar is supported in the Java API 11

12 The GregorianCalendar Concrete Class
We can use new GregorianCalendar() to construct a default GregorianCalendar object with the current time We can also use new GregorianCalendar(year, month, date) to construct a GregorianCalendar object with the specified year, month, and date The month parameter is 0-based, i.e., 0 is for January 12

13 get() in Calendar Class
get(int field) is used to extract detailed information from Calendar objects. The possible fields are: TestCalendar Run 13

14 Interfaces The Apple & Chicken classes are based on different superclasses, but both share a common behavior: both are edible! We can model this common behavior using an interface Interfaces can be used to specify common behavior for objects of unrelated classes as well as related classes Abstract classes model the common behavior of related classes only 14

15 interface & class Similarities
An interface is treated like a special class in Java Each interface is compiled into a separate bytecode file, just like a regular class Like an abstract class, we cannot create an instance from an interface using the new operator, but in most cases we can use an interface more or less the same way we use an abstract class For example, we can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa 15

16 Defining an interface Example:
An interface is a class-like construct that contains only constants and abstract methods public interface InterfaceName { constant declarations; abstract method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String howToEat(); } 16

17 interface inheritance
We can use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface (or inherit the interface) using the implements keyword Example: Chicken & Fruit implement the Edible interface in TestEdible Edible TestEdible Run 17

18 Omitting Modifiers in Interfaces
All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K) 18

19 Example: The Comparable Interface
This interface defines compareTo() for comparing objects compareTo() determines the order of this object with the specified object o and returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than o package java.lang; public interface Comparable<E> { public int compareTo(E o); } 19

20 compareTo() in Integer, BigInteger, String & Date
All numeric wrapper classes, Character and String implement Comparable, thus compareTo() is implemented in all of them 20

21 Example: compareTo() System.out.println(new Integer(3).compareTo(new Integer(5))); System.out.println("ABC".compareTo("ABE")); java.util.Date date1 = new java.util.Date(2013, 1, 1); java.util.Date date2 = new java.util.Date(2012, 1, 1); System.out.println(date1.compareTo(date2)); Displays -1 -2 1 21

22 The Comparable Interface
Let n be an Integer object, s be a String object and d be a Date object. Then, all the following expressions are true n instanceof Integer n instanceof Object n instanceof Comparable s instanceof String s instanceof Object s instanceof Comparable d instanceof java.util.Date d instanceof Object d instanceof Comparable 22


Download ppt "Chapter 13 Abstract Classes and Interfaces Part 01"

Similar presentations


Ads by Google