Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.

Slides:



Advertisements
Similar presentations
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
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.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Abstract Classes and Interfaces
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Abstract and Nested Classes
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Programming in Java CSCI-2220 Object Oriented Programming.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Object-Oriented Programming: Polymorphism Chapter 10.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
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.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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 7: Cloning and RTTI
Chapter 11 Abstract Classes and Interfaces
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 12 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

Chapter 14 Abstract Classes and Interfaces

Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract class provides a uniform interface for dealing with a family of objects Abstract classes are defined with the abstract keyword

Using abstract classes Concrete instances of abstract classes cannot be instantiated with the new operator However, variables of abstract class types can be declared and used to hold objects that extend (inherit from) the abstract class Abstract classes can have constructors that are called by subtypes using the super keyword (even though they cannot be created)

Abstract Methods Methods can be defined using the abstract keyword If a class contains abstract methods, the class must be declared abstract as well Abstract methods do not have a body Abstract methods are implemented by subclasses If a subclass does not implement all abstract methods in the superclass, it must be declared abstract

What is the purpose? Abstract methods define a common interface that all subclasses will implement The methods can be called on a supertype variable using polymorphism without regard to what actual subtype is stored in the variable. Allows generic treatment of a family of subtypes

Other points of interest An abstract class does not have to define any abstract methods A subclass can be defined abstract even if the superclass is concrete (e.g. java.lang.Object) A subclass can override a superclass method and define it to be abstract

Interfaces Interfaces are like abstract classes—but Interfaces are declared with the interface keyword Interfaces can only define abstract methods Interfaces can only declare constants Interfaces are compiled into separate byte- code files just like classes

How to use interfaces A class can extend only one class by inheritance (using the extends keyword) A class can implement an unlimited number of interfaces (using the implements keyword) An interface is “contract” of functionality Classes can be designed to use and provide specific functionality via an defined interface (the Application Programming Interface)

Java and Multiple Inheritance Java does not support inheriting from multiple classes (linear inheritance model) This avoids many problems encountered in C++ Java can simulate multiple inheritance by having a class implement multiple interfaces that provide the functionality of multiple “class abstractions”

The Comparable interface public interface Comparable { public int compareTo(Object o); } Classes implementing this interface can be compared to each other to obtain an ordering relationship public class MyClass extends Object implements Comparable { public int compareTo(Object o) { // code to compare objects } }

An informative example

A Caveat compareTo() returns zero if two objects have the same ordering relationship Generally, this would imply that the overriden equals() method inherited from java.lang.Object would also return true for the two objects Often the implementations will be congruent, but not always

ActionListener ActionListener is an interface for handling GUI events such as button clicks A class that implements ActionListener overrides the actionPerformed() method to provide custom processing to respond to GUI events A GUI element “adds” an object implementing ActionListener and then sends events (e.g. button clicks) to the object for processing through the actionPerformed() method

How does ActionListener work? Explain how ActionListener works under the covers if students do not appear too restless or bored Is event handling in Java kind of like a “come from” statement (opposite of goto)? ActionListeners are often implemented as anonymous inner classes--but that is a topic for next semester!

The Cloneable Interface Cloneable is an empty interface (no methods or constants defined) Empty interfaces are called marker interfaces and signal that an object implements certain characteristics In this case, Cloneable indicates that the class overrides the clone() method defined for java.lang.Object

clone() The definition of clone() is: protected native Object clone() throws CloneNotSupportedException; Cloning relies on the underlying system to create a copy of an object—native code clone() is a protected method, but it can be made public when overriden clone() can throw an exception that you should handle

Shallow Copy The overriden method clone() can be implemented by calling super.clone() Implementing it this way does a shallow copy A shallow copy will copy the contents of each field of the object to the new object The value of primitive types are copied The references to objects are copied References will be equal even though the cloned objects are not equal

Deep Copy A deep copy creates duplicates of the objects referenced by fields rather than simply copying the references Deep copy is implemented by first calling super.clone() and then adding code to duplicate objects Deep copy is important if the two objects modify internal objects and should not be using the same objects

Multithreading Multithreading means running two different paths of execution simultaneously Java has two ways to do multithreading: the good way and the other way The good way is to implement the Runnable interface The other way is to extend the Thread class (if you need to override its methods)

Runnable The Runnable interface has just one method: void run(); // no parameters The run() method is where thread execution starts, which is effectively the main method for the thread A class that implements Runnable can create a Thread with itself as the parameter and run itself in a thread

An example of multithreading

Setting up and calling threads

Results of running multiple threads

Interface vs. Abstract Class Interface All variables must be public static final Constructors are not allowed All methods must be public abstract Abstract Class No restrictions on variables Constructors are allowed and can be invoked by subclasses No restrictions on methods

When to use An abstract class represents common functionality extracted from a family of related objects An interface represents common functionality that can be implemented by unrelated objects What are some examples typifying this division of usage?

Primitive Data Types as Objects All primitive data types can be represented as objects Doing this would make the language perfectly uniform It would also make the language very slow Wrapper Classes are the objects that “wrap” primitive data types Wrapper classes allow generic programming

Wrapper Classes Boolean Character Byte Short Integer Long Float Double

Number The numeric wrapper classes extend the abstract Number class Number has methods to convert a value to various primitive types Each wrapper class overrides the toString(), equals() and compareTo() methods Each wrapper class knows the MAX_VALUE and MIN_VALUE for the types it wraps

Conversion methods shortValue() intValue() longValue() floatValue() doubleValue() valueOf(String strValue) // returns an object initialized with the value specified in strValue

Parsing methods public static xyz parseXyz(String s) public static xyz parseXyz(String s, int radix) Static methods called on wrapper class Integer.parseInt(“536”, 16); Returns the primitive type (xyz) represented by the string Can choose a radix value of 2, 8, 10, or 16 (the default is 10)*

BigInteger and BigDecimal Java provides two classes for handling very large or very precise numbers BigInteger and BigDecimal can represent numbers of any size or precision* Both classes implement add(), subtract(), multiply(), divide(), and remainder() Both implement compareTo() Be careful with BigDecimal.divide()*

Example using big numbers

CS4a Lab 1 redux Calculating Pi using BigDecimal

Problems to consider Will this require doing arithmetic with more digits of precision that just 20? What kinds of cumulative errors have we discussed in class that need to be avoided? How many digits of calculation precision will be required to accurately define 20 digits of pi? How many terms need to be expanded?

Details Remember to have a cover sheet and include appropriate comments and printouts of the results. Assignment due: 4:30 p.m. November 1, 2012