CompSci 230 S Software Construction

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
Java Implementation: Part 1 S CompSci 230 Software Construction.
Object Oriented Programming
Interfaces.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Java Interfaces Overview Java Interfaces: A Definition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Java Implementation Software Construction Lecture 6.
Abstract classes and Interfaces. Abstract classes.
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,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Programming in Java CSCI-2220 Object Oriented Programming.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
CompSci 230 S Programming Techniques
Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
More About Inheritance & Interfaces
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Chapter 6 Inheritance.
Presentation transcript:

CompSci 230 S2 2015 Software Construction Abstract Classes & Interfaces

Agenda & Reading Topics: Reading Abstract Classes Interfaces Abstract Classes VS Interfaces Interfaces, revisited Extending Interfaces The Final keyword Reading The Java Tutorial: Abstract Methods and Classes Writing Final Classes and Methods Java 2 – The Complete Reference Abstract Classes: Chapter 8, page 216-221 Interfaces: Chapter 9, page 235-247 07

Rectangle, Circle and Triangle Example: ShapesDemo.java repeating Bad design! General methods: get(), getY(), setX(…), setY(…), translate(…) Specific methods: (need to know the type) draw(…), area() 07

Shape Use of Inheritance : Add a new class : Shape Point Triangle Square Circle Use of Inheritance : Code Reuse Add a new class : Shape Common Instance variables: Point p (top left corner) Methods: set, get and translate (common to all shapes) Specific methods : draw, area Need to draw/calculate an area for all shapes But this is done differently for different shapes Use an array of Shape Create an array to store different shapes Shape.Area()/Shape.Draw()? It might not be appropriate to have any code for the area() method in the Shape class at all (you don’t know how to calculate the area of an object if you don’t know the type of the shape). public class Shape { protected Point p; //top-left corner public int getX() { return p.x; } ... Create an array Shape[] s = new Shape[3]; S[0] = new Rectangle(…); … for (int i =0; i< s.length; i++) { s[i].draw(g); } Invoke the draw method public class Shape { ... public void draw(Graphics g){?????? public abstract int area(){??????? Need to Change 07

Abstract classes ... 07 public abstract class Shape { … public abstract void draw(Graphics g); public abstract int area(); } Remove the code inside the area() method and make the method abstract Example: abstract/ShapesDemo.java Abstract methods Provide implementation in subclasses public class Rectangle extends Shape { private int width, height; public int area() { return (width * height); } ... public class Circle extends Shape { private int radius; public int area() { return (int) (Math.PI * radius * radius); } ... public class Triangle extends Shape { private int width, height; public int area() { return (width * height) / 2; } ... 07

Abstract Classes Aim: What goes in an abstract class? How? Rules Abstract classes permit declaration of classes that define only part of an implementation, leaving the subclasses to provide the details What goes in an abstract class? Implementation of common state and behaviour that will be inherited by subclasses Declaration of generic behaviours that subclasses must implement How? By using the abstract keyword in the class header and in any generic (abstract) methods Rules An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. An abstract method has no implementation When a class inherits from an abstract class, it must implement every abstract member defined by the abstract class An abstract class cannot be instantiated, however references to an abstract class can be declared. Shape s = new Rectangle(10, 20, 30, 40); Shape s = new Shape(); <- wrong 07

Java Interfaces If an abstract class contains only abstract method declarations, it should be implemented as an interface instead. Java Interface Defines a protocol of behaviour that can be implemented Defines a set of methods but does not implement them (no actual implementation) Contains: Method headers (signatures) Field definitions (Must be class constants: public, static, final only) is never directly instantiated Example: Interface in everyday life: VCR Methods are implicitly abstract, so the abstract keyword is not required in a method declaration public interface VCR { public boolean loadTape(String tapeName); public boolean eject(); public boolean play(); public boolean pause(); public boolean stop(); ... public static final int PLAYING=0, PAUSED=1, STOPPED=2, FORWARD=3, BACKWARD=4, NOTAPE=5; } 07

Example 1 Bounceable Interface Hitwall(String wallname) Example: Basketball.java Bounceable Interface Hitwall(String wallname) public class Basketball implements Bounceable { public void hitWall(String wallName) { System.out.println("I hit " + wallName); System.out.println("G is " + GRAVITY); } public static void main(String[] args) { Basketball b = new Basketball(); b.hitWall("Wall A"); Data fields are implicitly public, static and final public interface Bounceable { double GRAVITY = 9.8; void hitWall(String wallName); } Methods are implicitly abstract 07

Example 2 - MouseListener MouseListener Interface The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component MouseMotionListener Interface The listener interface for receiving mouse motion events on a component public interface MouseListener ... { mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) } public interface MouseMotionListener ... { mouseDragged(MouseEvent e) mouseMoved(MouseEvent e) } 07

Abstract classes Vs Interfaces An interface cannot implement any methods, whereas an abstract class can. (i.e. An interface is where all the methods are abstract) A class that implements an interface must either provide definitions for all methods or declare itself abstract Classes can extend only one class, but can implement one or more interfaces Unrelated classes can implement the same interface Interface Continuum Completely abstract class or interface Concrete super class Abstract class (At least one method is abstract) Use extends to make subclass A subclass implements the interface Use extends to make subclass 07

Interfaces, revisited An Interface is like a Class, with no bodies in the methods. It may define constants (public static final) but no runtime variables. Usually, an Interface is public. An interface provides a standard way to access a class which could be implemented in many different ways. The Java Tutorials: “There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a ‘contract’ that spells out how their software interacts.” “Each group should be able to write their code without any knowledge of how the other group's code is written.” “Generally speaking, interfaces are such contracts.” 07

Implementations, in Java In Java, a class which implements an interface must provide an implementation of every method defined within the interface A class may implement some additional methods (but these extra methods aren’t accessible through this interface) Beware: adding another method to an existing Interface will “break” every current implementation of this Interface! A class can implement many interfaces. An Interface can extend other Interfaces. Extension is the preferred way to add new methods to an Interface. (Do you understand why?) In Java, classes are less extendible than interfaces, because a Class can extend at most one other Class (“single inheritance”). 07

Example 3: Cups Given a family of classes, polymorphism allows you to treat a subclass object as if it were a superclass object create a single method that could wash any kind of cup in your virtual café. The prepareACup() method can invoke wash() on many different objects public static void prepareACup(Cup cup) { ... cup.wash(); class EspressoCup extends CoffeeCup { public void wash() { ... class Cup { public void wash() { ... class CoffeeMug extends CoffeeCup { public void wash() { ... class CoffeeCup extends Cup { public void wash() { ... 07

doesn't use polymorphism Washable? What if you wanted to have a method that can wash any kind of object ("washable" object)? For example: wash a window, wash your car, or wash a dog Objects don't seem to fit into the same family You may want to… class Dog{ public void wash() { ... class Window { public void wash() { ... class Car { public void wash() { ... class Cleaner { public static void cleanAnObject(Object obj) { if (obj instanceof Cup) { ((Cup) obj).wash(); } else if (obj instanceof Dog) { ((Dog) obj).wash(); } else if ... doesn't use polymorphism 07

WashableObject family Four families--cups, cars, windows, and dogs--are combined into the WashableObject family Full benefit of polymorphism in the cleanAnObject() method However .. What exactly is a washable object? What does one look like? Washable is an adjective, not a noun. It describes a behaviour exhibited by objects, not an object itself abstract class WashableObject { public abstract void wash(); } class Window extends WashableObject { public void wash() { ... class Dog extends WashableObject { public void wash() { ... class Cleaner { public static void cleanAnObject(WashableObject wo) { //... wo.wash(); 07

Washable Interface Interfaces in Java allow you to get the benefits of polymorphism without requiring you to build a singly-inherited family of classes. Although a class can extend only one other class, it can "implement" multiple interfaces. Solution Family of classes for cups, another for animals, one for vehicles (including cars), one for parts of a building etc Then each washable class can implement the Washable interface. 07

Extending Interfaces Interfaces can extend other interfaces, which brings rise to sub-interfaces and super-interfaces Interface extends interface, but a class cannot extends an interface. Unlike classes, however, an interface can extend more than one class at a time Another good reason to extend interfaces is that INTERFACES CANNOT GROW. Think about when you add methods to a class that’s already being used by other classes. It doesn’t effect them really, they can still use your class just as before. But, if you add methods to an interface, you break the classes that use this interface, since they now must implement another method. So, when something else is required in an interface, it is best to extend that interface, add the new method and then use the new sub-interface. public interface Displayable extends Drawable, Printable { //additional constants and abstract methods ... } 07

Example 4 public interface Bounceable { double GRAVITY = 9.8; void hitWall(String wallName); void AddMethod(); } The new method All classes that implement the old Bounceable interface will break because they don't implement all methods of the interface anymore. Solution: write a new sub-Interface Now users of your code can choose to continue to use the old interface or to upgrade to the new interface public interface Bounceable { double GRAVITY = 9.8; void hitWall(String wallName); } public interface BounceablePlus extends Bounceable { void AddMethod(); } 07

Typing Rules The typing rules for interfaces are similar to those for classes. A reference variable of interface type T can refer to an instance of any class that implements interface T or a sub-interface of T. Through a reference variable of interface type T, methods defined by T and its super interfaces can be called. interface I1 { public void m1(); public void m2(); } class c1 implements I1 { public void m1() {} public void m2() {} } inteface I2 extends I1 { public void m3(); } C1 a = new C1(); C1 b = new C1(); I1 p = a; p.m1(); p.m2(); C2 c = new C2(); I2 q = c; q.m1(); q.m2() q.m3(); a is a reference variable of type C1 p is a reference variable of type I1 class c2 implements I2 { public void m1() {} public void m2() {} public void m3() {} } q is a reference variable of type I2 07

instanceof You can use the instanceof operator to test an object to see if it implements an interface, before you invoke a method in this interface. This might improve readability and correctness. This might be a hack. Where possible, you should extend classes and interfaces to obtain polymorphic behaviour, rather than making a runtime check. if( b instanceof Bounceable ) { b.hitWall( "Wall A" ); } 07

Interfaces, in UML Interfaces specify behaviour (a public contract), without data or implementation. Interfaces are drawn like classes, but without attributes, and with the keyword <<Interface>> A dotted open-triangle arrow, from a class to an interface, means that “the class implements this interface”. We also say that “the class fulfils the contract specified by this interface”, or that it “realizes the interface.” Note that interfaces define methods but not attributes. A password allows a secureLogin(). 07

Final The final keyword can be applied to prevent some of the inheritance effects Final field: constant Final argument: cannot change the data within the called method Final method: cannot override method in subclasses Final class: cannot be subclassed (all of its methods are implicitly final as well) class ChessAlgorithm { . . . final void nextMove(ChessPiece pieceMoved, BoardLocation newLocation) { ... } 07

Interfaces in Java 8 In Java 8, an interface may contain implementations of static methods, and default implementations of instance methods 07

Default methods It allows developer to add new methods to the Interfaces without breaking the existing implementation of these Interface. Java 8 allows default methods to be added to interfaces with their full implementation Classes which implement the interface don’t have to have implementations of the default method interface A { public default void foo(){ System.out.println("Calling A.foo()"); } class AImpl implements A { public void existingMethod() { System.out.println("existing"); } AImpl obj = new AImpl(); obj.foo(); Output: Calling A.foo() 07

Static methods A static method is associated with a class not with the object. Static method works as helper method. So if we declare static methods in interface, it is easy for us to organize our helper methods. interface Village { void setNumOfPeople(int num); void setName(String name); static int getVillageId(){ return 1; } default String getBusinessType(){ return "Business type is Farming and village id:"+getVillageId(); class Location implements Village { public int noOfPeople; public String name; ... public int getLocationId(){ return Village.getVillageId(); } Business type is Farming and village id:1 Village id:1 Location Id:1 Location lo = new Location(); System.out.println(lo.getBusinessType()); System.out.println("Village id:"+Village.getVillageId()); System.out.println("Location Id:"+lo.getLocationId()); 07