Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes and Interfaces Chapter 9 CSCI 1302.

Similar presentations


Presentation on theme: "Abstract Classes and Interfaces Chapter 9 CSCI 1302."— Presentation transcript:

1 Abstract Classes and Interfaces Chapter 9 CSCI 1302

2 CSCI 1302 – Abstract Classes and Interfaces2 Outline Introduction Abstract Classes Interfaces –Implementing Interfaces –Interfaces vs. Abstract Classes –Creating Custom Interfaces Processing Primitive Data Type Values as Objects –Numeric Wrapper Class Constructors

3 CSCI 1302 – Abstract Classes and Interfaces3 Outline Processing Primitive Data Type Values as Objects (cont.) –Numeric Wrapper Class Constants –Conversion Methods –The Static valueOf Methods –The Methods for Parsing Strings into Numbers Automatic Conversion Between Primitive and Wrapper Class Types

4 CSCI 1302 – Abstract Classes and Interfaces4 Introduction Recall specificity chain Superclasses should contain common features of its subclasses Superclass so general it does not have specific instances is an abstract class Java classes can have only one superclass (single inheritance) Can emulate multiple inheritance with interfaces

5 CSCI 1302 – Abstract Classes and Interfaces5 Abstract Classes Concrete classes vs. abstract classes Classes that are derived from the same superclass have common properties and behaviors Often the superclass will have methods (behaviors) that it cannot and should not implement for its subclasses These methods are abstract methods, method signature in the superclass, but only implemented in the subclass

6 CSCI 1302 – Abstract Classes and Interfaces6 Abstract Classes A class that contains abstract methods should be declared abstract Cannot declare new instances of this class Provides common features (data and methods) Declares subclass implementation-specific methods as abstract

7 CSCI 1302 – Abstract Classes and Interfaces7 Abstract Classes Abstract methods must be in abstract classes If a subclass does not implement all abstract superclass methods, the subclass must be abstract Abstract classes cannot be instantiated, but can still have constructors for its subclasses

8 CSCI 1302 – Abstract Classes and Interfaces8 Abstract Classes See GeometricObject code packet

9 CSCI 1302 – Abstract Classes and Interfaces9 Interfaces Classlike construct that contains only constants and abstract methods Declared with interface keyword public interface InterfaceName { /** Constants */ /** Method signatures */ } Provides a generic framework for common operations Best example: java.lang.Comparable

10 CSCI 1302 – Abstract Classes and Interfaces10 Interfaces Interface can define generic compareTo method package java.lang; public interface Comparable { public int compareTo(Object o); } Defined behavior: Returns 0 if equal, negative number if calling object is less than o, positive number if calling object is greater than o

11 CSCI 1302 – Abstract Classes and Interfaces11 Interfaces Can now create generic max method public Object max(Object o1, Object o2) { if(((Comparable)o1).compareTo(o2) > 0) return o1; else return o2; }

12 CSCI 1302 – Abstract Classes and Interfaces12 Implementing Interfaces Use keyword implements when forming class Inherits all constants in the interface and implements all the methods Another form of generic programming

13 CSCI 1302 – Abstract Classes and Interfaces13 Implementing Interfaces Create a ComparableRectangle class that implements Comparable See ComparableRectangle.java

14 CSCI 1302 – Abstract Classes and Interfaces14 Interface vs. Abstract Classes Interfaces and abstract classes can be used in similar ways, but are defined differently In an interface, the data must be constants, abstract classes can have non-constant data fields (variables) Interface methods have only a signature, abstract classes can have concrete methods

15 CSCI 1302 – Abstract Classes and Interfaces15 Interface vs. Abstract Classes Classes can only inherit from one class, but can implement many public class NewClass extends BaseClass implements Interface1, …, InterfaceN Both model common features, when do you use one over the other? Use inheritance when modeling strong is-a relationships. Use interfaces when modeling weak is-a relationships Interfaces are adjectives or nouns

16 CSCI 1302 – Abstract Classes and Interfaces16 Primitive Data Types as Objects Primitive data types exist for performance issues Some methods only take objects, use wrapper classes to wrap primitive data types into forms useful for generic programming Boolean, Character, Double, Float, Byte, Short, Integer, and Long in java.lang All extend the abstract Number class and implement the Comparable interface

17 CSCI 1302 – Abstract Classes and Interfaces17 Wrapper Class Constructors Construct from primitive type or string Integer intObj = new Integer(5); Integer intObj = new Integer(“5”); Double dblObj = new Double(5.0); Double dblObj = new Double(“5.0”);

18 CSCI 1302 – Abstract Classes and Interfaces18 Wrapper Class Constants All wrapper classes have MAX_VALUE and MIN_VALUE System.out.println(Integer.MAX_VALUE); System.out.println(Float.MIN_VALUE);

19 CSCI 1302 – Abstract Classes and Interfaces19 Conversion Methods doubleValue, floatValue, intValue, longValue, and shortValue methods are available in all wrapper classes long l = doubleObject.longValue(); int i = integerObject.intValue(); double a = 5.9; Double doubleObject = new Double(a); String s = doubleObject.toString();

20 CSCI 1302 – Abstract Classes and Interfaces20 Static valueOf Methods Convert string into object of wrapper type Double a = Double.valueOf(“12”); Integer b = Integer.valueOf(“4”);

21 CSCI 1302 – Abstract Classes and Interfaces21 Parsing Strings into Numbers Parse strings into with parseType methods Integer.parseInt(“10”); Long.parseLong(“12”); Byte.parseByte(“14”);

22 CSCI 1302 – Abstract Classes and Interfaces22 Sort Arrays of Objects See GenericSort.java


Download ppt "Abstract Classes and Interfaces Chapter 9 CSCI 1302."

Similar presentations


Ads by Google