SlideShare a Scribd company logo
Object Oriented Design
    and Programming II
        Chapter 10
    Abstract classes and
         Interfaces

03/25/13   Abstract classes & Interface   1
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
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?



03/25/13          Abstract classes & Interface   4
Abstract Class
 A class that cannot instantiate objects.

                          Message


            Text           Voice                         Fax
           Message        Message                      Message

                  Public abstract class Message {
                  }

           Do I and should I use non-abstract methods
           in abstract classes?
03/25/13                Abstract classes & Interface             5
Abstract or not-abstract
                 Contains                        Contains non-
                 abstract                        abstract
                 methods                         methods
Class abstract   May contain                     OK
                 abstract methods.               E.g. class
                 abstract methods                BJ_abstract_Figure
                 must be in abstract
                 classes
                 GeometricObject in
                 BJ_GeomObj.
                 Situation 3

Class non-       Not allowed                     Fine. You are
abstract
03/25/13          Abstract classes & Interface   familiar with this
                                                                  6
Example BJ_abstract_Figure
 Problems of Bj_FindArea
 BJ_abstract_Figure
          An abstract class used as supertype
          An object cannot be created from an abstract
           class
     
           An abstract class can be extended by a
           subclass



03/25/13                Abstract classes & Interface      7
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




03/25/13        Abstract classes & Interface   8
Example BJ_GeomObj_Circle9
 Circle9 extends an abstract class
  GeometricObject
 Note the 4 situations in the project:
     
           Circle class has concrete method getArea()
     
           No abstract method getArea(); concrete
           getArea() in Circle
     
           abstract method getArea(); concrete
           getArea() in Circle
          Instantiate object of abstract class

03/25/13              Abstract classes & Interface      9
Empty vs abstract methods
 Method with empty body
     
           protected abstract double getArea();
 Abstract method
          protected abstract double getArea();




03/25/13                Abstract classes & Interface   10
From abstract class

     To Interface
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.


03/25/13            Abstract classes & Interface    12
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.


03/25/13        Abstract classes & Interface   13
Multiple Inheritance
             Class A           Class B                Class 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.

03/25/13               Abstract classes & Interface             14
Defining Interfaces




03/25/13       Abstract classes & Interface   15
Interface Declaration




             public interface StockWatcher{ }

             public interface Sortable{ }
03/25/13            Abstract classes & Interface   16
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.
03/25/13        Abstract classes & Interface   17
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.

03/25/13         Abstract classes & Interface   18
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
              }
       }
}
    03/25/13               Abstract classes & Interface            19
Code Review
 BJ_Interface
     
           Objects inheriting properties of superclass
           and implementing properties of interface
          A class may have only one superclass but
           may implement multiple interfaces
     
           Using an array of supertype
          polymorphism



03/25/13                 Abstract classes & Interface    20
Sorting
   It is easy to write a sorting method for numbers
    of a specific type.
      
           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
   It is a challenge to write a method to sort
    objects
   How do you do the above in C?
03/25/13                   Abstract classes & Interface   21
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:ProgramFilesJava
    jdk1.5.0_12demoappletsSortDemo
       To compare speed
Application Programming
                Interface (API)
 How do I learn to use the packages in the
  Java platform?
 Answer: API
 http://java.sun.com/j2se/1.5.0/docs/api/




03/25/13          Abstract classes & Interface   24
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


03/25/13                Abstract classes & Interface   25
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

More Related Content

What's hot

8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Vinay Kumar
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Aae oop xp_03
Aae oop xp_03Aae oop xp_03
Aae oop xp_03Niit Care
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
Sérgio Souza Costa
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrptionMahi Mca
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
Mazharul Sabbir
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Interface in java
Interface in javaInterface in java
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 

What's hot (20)

8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Aae oop xp_03
Aae oop xp_03Aae oop xp_03
Aae oop xp_03
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
 
Vvi
VviVvi
Vvi
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrption
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Interface in java
Interface in javaInterface in java
Interface in java
 
7494605
74946057494605
7494605
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Java interface
Java interfaceJava interface
Java interface
 

Similar to 9 abstract interface

Opps
OppsOpps
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
G C Reddy Technologies
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
bindur87
 
Micro Anti-patterns in Java Code
Micro Anti-patterns in Java CodeMicro Anti-patterns in Java Code
Micro Anti-patterns in Java Code
Ganesh Samarthyam
 
abstract class and interface.Net
abstract class and interface.Netabstract class and interface.Net
abstract class and interface.Net
BGSBU Rajouri
 
BIT211_3.pdf
BIT211_3.pdfBIT211_3.pdf
BIT211_3.pdf
Sameer607695
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 
C# interview
C# interviewC# interview
C# interview
Thomson Reuters
 
C#
C#C#
C# interview questions
C# interview questionsC# interview questions
C# interview questions
Chetan Chaudhari
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Simplilearn
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
It Academy
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
JayasankarPR2
 

Similar to 9 abstract interface (20)

Opps
OppsOpps
Opps
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Java 06
Java 06Java 06
Java 06
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Micro Anti-patterns in Java Code
Micro Anti-patterns in Java CodeMicro Anti-patterns in Java Code
Micro Anti-patterns in Java Code
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
abstract class and interface.Net
abstract class and interface.Netabstract class and interface.Net
abstract class and interface.Net
 
Core java questions
Core java questionsCore java questions
Core java questions
 
BIT211_3.pdf
BIT211_3.pdfBIT211_3.pdf
BIT211_3.pdf
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
C# interview
C# interviewC# interview
C# interview
 
C#
C#C#
C#
 
C# interview questions
C# interview questionsC# interview questions
C# interview questions
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 

More from Abhijit Gaikwad (11)

Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
20 ch22 collections
20 ch22 collections20 ch22 collections
20 ch22 collections
 
17 sessions
17 sessions17 sessions
17 sessions
 
16 cookies
16 cookies16 cookies
16 cookies
 
15 decorator pattern
15 decorator pattern15 decorator pattern
15 decorator pattern
 
12 memory hierarchy
12 memory hierarchy12 memory hierarchy
12 memory hierarchy
 
12 cache questions
12 cache questions12 cache questions
12 cache questions
 
10 strategy pattern
10 strategy pattern10 strategy pattern
10 strategy pattern
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
 
7 inheritance
7 inheritance7 inheritance
7 inheritance
 
4 recursion details
4 recursion details4 recursion details
4 recursion details
 

9 abstract interface

  • 1. Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces 03/25/13 Abstract classes & Interface 1
  • 3. 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
  • 4. 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? 03/25/13 Abstract classes & Interface 4
  • 5. Abstract Class  A class that cannot instantiate objects. Message Text Voice Fax Message Message Message Public abstract class Message { } Do I and should I use non-abstract methods in abstract classes? 03/25/13 Abstract classes & Interface 5
  • 6. Abstract or not-abstract Contains Contains non- abstract abstract methods methods Class abstract May contain OK abstract methods. E.g. class abstract methods BJ_abstract_Figure must be in abstract classes GeometricObject in BJ_GeomObj. Situation 3 Class non- Not allowed Fine. You are abstract 03/25/13 Abstract classes & Interface familiar with this 6
  • 7. Example BJ_abstract_Figure  Problems of Bj_FindArea  BJ_abstract_Figure  An abstract class used as supertype  An object cannot be created from an abstract class  An abstract class can be extended by a subclass 03/25/13 Abstract classes & Interface 7
  • 8. 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 03/25/13 Abstract classes & Interface 8
  • 9. Example BJ_GeomObj_Circle9  Circle9 extends an abstract class GeometricObject  Note the 4 situations in the project:  Circle class has concrete method getArea()  No abstract method getArea(); concrete getArea() in Circle  abstract method getArea(); concrete getArea() in Circle  Instantiate object of abstract class 03/25/13 Abstract classes & Interface 9
  • 10. Empty vs abstract methods  Method with empty body  protected abstract double getArea();  Abstract method  protected abstract double getArea(); 03/25/13 Abstract classes & Interface 10
  • 11. From abstract class To Interface
  • 12. 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. 03/25/13 Abstract classes & Interface 12
  • 13. 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. 03/25/13 Abstract classes & Interface 13
  • 14. Multiple Inheritance Class A Class B Class 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. 03/25/13 Abstract classes & Interface 14
  • 15. Defining Interfaces 03/25/13 Abstract classes & Interface 15
  • 16. Interface Declaration public interface StockWatcher{ } public interface Sortable{ } 03/25/13 Abstract classes & Interface 16
  • 17. 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. 03/25/13 Abstract classes & Interface 17
  • 18. 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. 03/25/13 Abstract classes & Interface 18
  • 19. 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 } } } 03/25/13 Abstract classes & Interface 19
  • 20. Code Review  BJ_Interface  Objects inheriting properties of superclass and implementing properties of interface  A class may have only one superclass but may implement multiple interfaces  Using an array of supertype  polymorphism 03/25/13 Abstract classes & Interface 20
  • 21. Sorting  It is easy to write a sorting method for numbers of a specific type.  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  It is a challenge to write a method to sort objects  How do you do the above in C? 03/25/13 Abstract classes & Interface 21
  • 22. Example: Comparable interface  Java.lang.Comparable  See BJ_Max  See BJ_GenericSort  Still relying on Java 1.5 unboxing feature of wrapper objects
  • 23. 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:ProgramFilesJava jdk1.5.0_12demoappletsSortDemo  To compare speed
  • 24. Application Programming Interface (API)  How do I learn to use the packages in the Java platform?  Answer: API  http://java.sun.com/j2se/1.5.0/docs/api/ 03/25/13 Abstract classes & Interface 24
  • 25. 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 03/25/13 Abstract classes & Interface 25
  • 26. 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
  • 27. 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