Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java Programming

Similar presentations


Presentation on theme: "Advanced Java Programming"— Presentation transcript:

1 Advanced Java Programming
Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11

2 Chapter Goals To be able to declare and use interface types
To appreciate how interfaces can be used to decouple classes

3 Why Interface A single motor is a reusable component.
How about reusable software components?

4 Why Interface Inheritance?
Session #, Speaker Name Why Interface Inheritance? Java does not allow you to inherit from more than one class (Superclass) Separating reusable part -- Interface methods A class will implements the interface methods. Use more than one reusable component but these are not related to each other. So can not be put in a single superclass. 08/24/11

5 Why Interface Interfaces are used to express the common operations.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. No implementations for these common behaviors

6 Interface An abstract type that contains no data, but exposes behaviors defined as methods. A class can only extends to one super abstract class, but can implements more than one interfaces) Composed by public static final attributes and/or method(s) without implementation.

7 Defining an interface Interface types are used to express the common operations.

8 Defining an interface Therefore, can implement a reusable average method:

9 Implementing an interface
A class implements an interface type if it declares the interface in an implements clause. The class should then implement the method or methods that the interface requires

10 Implementing an interface
Method(s) in interface are public, abstract by default. Don’t forget to declare the methods’ modifier in the implemented class. Interface cannot be instantiated. Variables in Interface are public static final by default. Method(s) in subclass cannot have narrower modifier than the method(s) in interface.

11 Implementing an interface

12 Interface VS. Inheritance
a class can implement multiple interfaces a class can only extend a single superclass. interface provides methods without implementation superclass provides some implementation that a subclass inherits developing interfaces when you have code that processes objects of different classes in a common way. using inheritance when have objects with something in common.

13 Interface VS. Abstract class
An interface cannot provide any code at all, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. A Class may implement several interfaces. In case of abstract class, a class may extend only one abstract class. All methods of an Interface are abstract. An abstract class can have non-abstract methods. An Interface cannot have instance variables. An abstract class can have instance variables. An Interface visibility must be public (or) none. An abstract class can have any visibility: public, private, protected. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. An Interface cannot contain constructors. An abstract class can contain constructors. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.

14 Abstract, Interface and Inheritance
A superclass can be an abstract class (without implementing some methods), and also can be a regular class with implementations for all methods in it. An Interface type always be a abstract class without any implementation of methods ….. You list more!

15 Common Error

16 Common Error

17 Constants in Interfaces
Interface can not have instance variable But can have constants The constant in an interface is public, static and final by default, you could omit these reserved words.

18

19

20

21 Announcement For some reason, you know what it is, the due date of programming assignment 2 is extended to Feb 4 (next Tuesday)!


Download ppt "Advanced Java Programming"

Similar presentations


Ads by Google