Abstract Classes and Interfaces Chapter 9 CSCI 1302.

Slides:



Advertisements
Similar presentations
Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Inheritance using Java
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 9 Abstract Classes and Interfaces
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Chapter 5 Abstract Classes and Interfaces. 2 Objectives u To design and use abstract classes. u To process a calendar using the Calendar and GregorianCalendar.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
1 P ART 4 A BSTRACT C LASSES AND I NTERFACES, W RAPPER C LASSES, AND H ANDLING E XCEPTIONS 1.
1 1 Chapter 14 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
Interfaces Professor Evan Korth.
Chapter 13 Abstract Classes and Interfaces
CMSC 202 Static Methods.
Unit-2 Objects and Classes
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 10 Thinking in Objects Part 2
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 10 Thinking in Objects
Presentation transcript:

Abstract Classes and Interfaces Chapter 9 CSCI 1302

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

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

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

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

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

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

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

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

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

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; }

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

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

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

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

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

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”);

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);

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();

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”);

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”);

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