Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.

Similar presentations


Presentation on theme: "Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA."— Presentation transcript:

1 Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA

2 Midterm  Thursday, 5/5/2016  20% of your grade  Covers material from lectures 2 – 6 (includes this one)  Two parts:  Part 1 (45 pts): written, closed books/notes/electronics/everything  Basic ideas from lecture slides, I will ask you write code snippets.  Bring your own pen, I will provide scratch paper  Part 2 (55 pts): Programming  Open everything except no communications with others  Turn in part 1 first, then work on part 2  You have the entire class to work on both parts

3 Reading for this week’s lecture:  Oracle tutorial on Interfaces and Inheritance:  https://docs.oracle.com/javase/tutorial/java/IandI/index.html https://docs.oracle.com/javase/tutorial/java/IandI/index.html  MIT OpenCourseware:  http://ocw.mit.edu/courses/civil-and-environmental-engineering/1-00- introduction-to-computers-and-engineering-problem-solving-spring- 2012/recitations/MIT1_00S12_REC_6.pdf http://ocw.mit.edu/courses/civil-and-environmental-engineering/1-00- introduction-to-computers-and-engineering-problem-solving-spring- 2012/recitations/MIT1_00S12_REC_6.pdf  University of Western Ontario:  http://www.csd.uwo.ca/~watt/home/courses/2011- 12/cs1025a/Notes/CS1025%2014%20--%20Intro%20to%20Java%20V%20-- %20abstract%20classes%20and%20interfaces.pdf http://www.csd.uwo.ca/~watt/home/courses/2011- 12/cs1025a/Notes/CS1025%2014%20--%20Intro%20to%20Java%20V%20-- %20abstract%20classes%20and%20interfaces.pdf

4 Why Interfaces?  Suppose you have a team of 400 software developers working on the same project, how do you ensure that each person’s code would work with each other?  It’ll be very difficult for each developer to understand how the other 399 developers wrote their code in order to integrate all the different parts.  What we need is some kind of contract so that people know exactly what each other are doing  The contract should define the behavior of the code so that everybody is on the same page.

5 What is an interface?  A reference type, similar to a class  Contains only constants, method signatures, default methods, static methods, and nested types.  Method bodies exist only for default methods and static methods.  Cannot be instantiated—they can only be implemented by classes or extended by other interfaces.  Used for APIs (Application Programming Interface)  Hides implementation from the user

6 Example of an Interface public interface DriveCar { int DRIVER_NUM = 20; void turn(String direction); int signalTurn(String direction, boolean signalOn); }

7 7 7 Define an Interface To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface InterfaceName { constant declarations; abstract method signatures; } Example : public interface Edible { /** Describe how to eat */ public abstract String howToEat(); }

8 Implementing an Interface  To declare a class that implements an interface, you include an implements clause in the class declaration.  Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. This is java’s way of providing “multiple inheritance”  By convention, the implements clause follows the extends clause, if there is one.

9 Problem of Multiple Inheritance  Both B and C overrides m1  D inherits from both B and C  When you call D.m1(), which method implementation does it use?

10 10 Omitting Modifiers in Interfaces All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K).

11 11 Example You can now use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface using the implements keyword. For example, the classes Chicken and Fruit implement the Edible interface.

12 12 Interface is a Special Class  An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class.  You cannot create an instance from an interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class.  For example, you can use an interface as a data type for a variable, as the result of casting, ArrayList types, and so on.

13 Default Methods and Static Methods  A Default Method allows interfaces to specify method body without the client having to implement it.  Allows you to add functionality to interfaces without breaking other people’s code  Static methods are shared between all the client classes.

14 14 Example: The Comparable Interface // This interface is defined in // java.lang package package java.lang; public interface Comparable { public int compareTo(E o); }

15 15 The toString, equals, and hashCode Methods Each wrapper class overrides the toString, equals, and hashCode methods defined in the Object class. Since all the numeric wrapper classes and the Character class implement the Comparable interface, the compareTo method is implemented in these classes.

16 16 Integer and BigInteger Classes String and Date Classes

17 17 Example 1 System.out.println(new Integer(3).compareTo(new Integer(5))); 2 System.out.println("ABC".compareTo("ABE")); 3 java.util.Date date1 = new java.util.Date(2013, 1, 1); 4 java.util.Date date2 = new java.util.Date(2012, 1, 1); 5 System.out.println(date1.compareTo(date2));

18 18 Generic sort Method Let n be an Integer object, s be a String object, and d be a Date object. All the following expressions are true. The java.util.Arrays.sort(array) method requires that the elements in an array are instances of Comparable.

19 19 Defining Classes to Implement Comparable

20 20 Abstract Classes and Abstract Methods

21 abstract method in abstract class  An abstract method cannot be contained in a non-abstract class.  If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract.  In other words, in a non-abstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. 21

22 object cannot be created from abstract class  An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses.  For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class. 22

23 abstract class without abstract method  A class that contains abstract methods must be abstract.  However, it is possible to define an abstract class that contains no abstract methods.  In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass. 23

24 superclass of abstract class may be concrete  A subclass can be abstract even if its superclass is concrete.  For example, the java.lang.Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. 24

25 concrete method overridden to be abstract  A subclass can override a method from its superclass to define it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined abstract. 25

26 26 abstract class as type You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct. GeometricObject[] geo = new GeometricObject[10];

27 The Java Collections Framework: Common Classes


Download ppt "Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA."

Similar presentations


Ads by Google