9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
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.
UML Class Diagram: class Rectangle
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Inheritance using Java
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Polymorphism & Interfaces
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
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.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
 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.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
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,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
2 Interfaces and Packages 3 What is an Interface? An interface defines a protocol of behavior that can be implemented by any class anywhere in the class.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
More About Java and Java How to Program By Deitel & Deitel.
Sections Inheritance and Abstract Classes
University of Central Florida COP 3330 Object Oriented Programming
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Chapter 13 Abstract Classes and Interfaces
One class is an extension of another.
Extending Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Abstract Classes Page
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Presentation transcript:

9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces

Software Engineering Principle   Minimize change

From concrete classes   To abstract classes   To interface   Examples: BJ_1_initial_SalCal BJ_2_class_SalCal BJ_3_abstract_SalCal BJ_4_abstract_method_SalCal BJ_5_interface_SalCal

9/4/2015Abstract classes & Interface4 abstract and final Methods  An abstract method in a superclass has no implementation, and it is to be overridden by a method in its subclass.  A final method in a superclass cannot be overridden at its subclass.  Why do I need to bother with abstract methods and final methods? Can I live happily in Java without them?

9/4/2015Abstract classes & Interface5 Abstract Class  A class that cannot instantiate objects. Message Text Message Voice Message Fax Message Public abstract class Message { } Do I and should I use non-abstract methods in abstract classes?

9/4/2015Abstract classes & Interface6 Abstract or not-abstract Contains abstract methods Contains non- abstract methods Class abstract May contain abstract methods. abstract methods must be in abstract classes GeometricObject in BJ_GeomObj. Situation 3 OK E.g. class BJ_abstract_Figure Class non- abstract Not allowed Fine. You are familiar with this

9/4/2015Abstract classes & Interface7 Example BJ_abstract_Figure  Problems of Bj_FindArea  BJ_abstract_Figure An abstract class used as supertype An abstract class used as supertype An object cannot be created from an abstract class An object cannot be created from an abstract class An abstract class can be extended by a subclass An abstract class can be extended by a subclass

9/4/2015Abstract classes & Interface8 Example BJ_abstract_Figure2  An abstract class used as supertype  An object cannot be created from an abstract class  An array of the abstract type is used to contain objects of the concrete subclasses

9/4/2015Abstract classes & Interface9 Example BJ_GeomObj_Circle9  Circle9 extends an abstract class GeometricObject  Note the 4 situations in the project: Circle class has concrete method getArea() Circle class has concrete method getArea() No abstract method getArea(); concrete getArea() in Circle No abstract method getArea(); concrete getArea() in Circle abstract method getArea(); concrete getArea() in Circle abstract method getArea(); concrete getArea() in Circle Instantiate object of abstract class Instantiate object of abstract class

9/4/2015Abstract classes & Interface10 Empty vs abstract methods  Method with empty body protected abstract double getArea(); protected abstract double getArea();  Abstract method protected abstract double getArea(); protected abstract double getArea();

From abstract class To Interface

9/4/2015Abstract classes & Interface12 What is Interface  An interface is a named collection of method definitions and constants ONLY.  An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy.  An interface defines a set of methods but does not implement them.  A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behaviors.

9/4/2015Abstract classes & Interface13 Interface and Abstract Classes  An interface cannot implement any methods, whereas an abstract class can.  A class can implement many interfaces but can have only one superclass.  An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.

9/4/2015Abstract classes & Interface14 Multiple Inheritance Class AClass BClass C Class ABC Class ABC inherits all variables and methods from Class A, Class B, and Class C. Java does NOT support multiple inheritances. However, you can use interface to implement the functionality of multiple inheritance.

9/4/2015Abstract classes & Interface15 Defining Interfaces

9/4/2015Abstract classes & Interface16 Interface Declaration public interface StockWatcher{ } public interface Sortable{ }

9/4/2015Abstract classes & Interface17 Interface Body  The interface body contains method declarations for ALL the methods included in the interface.  A method declaration within an interface is followed by a semicolon (;) because an interface does not provide implementations for the methods declared within it.  All methods declared in an interface are implicitly public and abstract.

9/4/2015Abstract classes & Interface18 Implement an Interface  An interface defines a protocol of behavior.  A class that implements an interface adheres to the protocol defined by that interface.  To declare a class that implements an interface, include an implements clause in the class declaration.

9/4/2015Abstract classes & Interface19 Implement Interface (Example) public class StockApplet extends Applet implements StockWatcher {... public void valueChanged(String tickerSymbol, double newValue) { if (tickerSymbol.equals(sunTicker)) { // record newValue for sunTicker... } else if (tickerSymbol.equals(oracleTicker)) { // record newValue for oracleTicker } else if (tickerSymbol.equals(ciscoTicker)) { // record newValue for ciscoTicker }

9/4/2015Abstract classes & Interface20 Code Review  BJ_Interface Objects inheriting properties of superclass and implementing properties of interface Objects inheriting properties of superclass and implementing properties of interface A class may have only one superclass but may implement multiple interfaces A class may have only one superclass but may implement multiple interfaces Using an array of supertype Using an array of supertype polymorphism polymorphism

9/4/2015Abstract classes & Interface21 Sorting  It is easy to write a sorting method for numbers of a specific type. bubblesort, shellsort, quicksort, heapsort. bubblesort, shellsort, quicksort, heapsort.  It is not easy to write a method to sort numbers of any primitive type: short, int, long, float, and double. See Example 9.2 GenericSort.java See Example 9.2 GenericSort.java  It is a challenge to write a method to sort objects  How do you do the above in C?

Example: Comparable interface   Java.lang.Comparable   See BJ_Max   See BJ_GenericSort Still relying on Java 1.5 unboxing feature of wrapper objects

Source Code Review and Demo  Exercise 1: Download, run, and study BJ_Sort/SortTest.java  Each class needs to implement the Sortable interface with a compare() method  Exercise 2: Add other sorting methods Bubble, Insertion, Selection  Exercise 3: C:\ProgramFiles\Java\ jdk1.5.0_12\demo\applets\SortDemo To compare speed To compare speed

9/4/2015Abstract classes & Interface24 Application Programming Interface (API)  How do I learn to use the packages in the Java platform?  Answer: API 

9/4/2015Abstract classes & Interface25 API Study example: StringTokenizer 1.Location of the API( package) 2.Class definition 3.Constructor(s) – usually more than one 4.Methods (you can see public only) 5.Variables (any public ones) 6.Interfaces – supply your own methods 7.Exceptions 8.Examples

Clone()   newObject = someObject; Only assigns the reference of someObject to newObject. No copy is made   newObject = someObject.clone(); Copies someObject to a new memory location   See BJ_House

hashCode()   hashCode() returns the object’s hash code   Hash code is an integer to store the object in a hash set.   If you override the equal() method, you should also override the hashCode   See BJ_House