Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail,

Similar presentations


Presentation on theme: "Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail,"— Presentation transcript:

1 Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of , complex details such as what happens as soon as you send an , the protocol your server uses are hidden from the user. Therefore, to send an you just need to type the content, mention the address of the receiver, and click send. Likewise in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. In Java, abstraction is achieved using Abstract classes and interfaces.

2 Abstract Class Declaring an abstract class Extending an abstract class
Keyword abstract must be included in the class declaration Ex: public abstract class NameofAbstractClass Extending an abstract class public class NameOfSubclass extends NameOfAbstractClass() Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. An abstract class is similar to Java classes yet different. An abstract class must have the keyword abstract in its class declaration. It does not contain any constructors but should contain abstract methods. Abstract methods are methods that have the keyword abstract in the method declaration but do not contain any code. Only the method declaration is stated. The method does not have a method body.

3 Interfaces An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

4 Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish, as a formal contract, a set of methods that a class will implement A Java interface is a collection of constants and abstract methods. An abstract method is a method that does not have an implementation. That is, there is no body of code defined for an abstract method. The head of the method, including its parameter list, is imply followed by a semicolon. An interface cannot be instantiated.

5 Interfaces interface is a reserved word None of the methods in
an interface are given a definition (body) public interface Doable { public void doThis(); public int doThat(); public void doThis2 (double value, char ch); public boolean doTheOther (int num); } A semicolon immediately follows each method header

6 Interfaces An interface cannot be instantiated
Methods in an interface have public visibility by default A class formally implements an interface by stating so in the class header providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

7 Interfaces public class CanDo implements Doable {
public void doThis () // whatever } public void doThat () // etc. implements is a reserved word Each method listed in Doable is given a definition The interface declaration contains the keyword interface and does not contain the keyword class. The class that implements the interface is responsible for carrying out the code the methods defined in the interface.

8 Interfaces A class that implements an interface can implement other methods as well In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

9 Complexity.java An interface is a collection of abstract methods,. It cannot be instantiated. Complexity.java represents the interface for an object that can be assigned an explicit complexity. A class implments an interface, which formally defines a set of methods used to interact with objects in that class.

10 Question.java The Question class shown here implements the Complexity interface. Both the setComplexity and getComplexity methods are implemented. They must be declared with the same signatures as the abstract methods (setComplexity and getComplexity) in the interface. In the Question class, the methods are defined simply to set or return a numeric value representing he complexity level of the question that the object represents. Note that the Question class also implements methods that are not part of the Complexity interface, methods called getQuestion, getAnswer, answerCorrect, and toString, which have nothing to do with the interface. The interface guarantees that the class implements certain methods, but it can also have other methods and usually does.

11 Interfaces A class can implement multiple interfaces
The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header Several classes can implment the same interface, giving different definitions for the methods. For example, we could implement a class called ToDo that also implements the Complexity interface. In it, we could choose to manage the complexity of the ToDo list in a different way but it would still have to implement all of the methods of the interface. class ManyThings implements interface1, interface2 { // all methods of both interfaces }

12 Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains an abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order The List interface is implemented by classes that represent an ordered collection of elements. The Iterator interface contains methods that allow the user to move easily through a collection of objects A class can implement more than one interface. The class must implement all methods in all interface listed. To show that a class implements multiple interfaces, we list them in implements clause, separated by commas. In addition to, or instead of, abstract methods, an interface can also contain constants, defined using the final modifier. When a class implements an interface, it can use all of the constants defined in it. This allows multiple classes to share a set of constants.

13 The Comparable Interface
The Comparable interface provides a common mechanism for comparing one object to another if (obj1.compareTo(obj2) < 0) System.out.println (“obj1 is less than obj2”); The result is negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer writes a class that implements the Comparable interface, it should follow this intent It's up to the programmer to determine what makes one object less than another The Comparable interface is defined in the java.lang package. It contains only one method, compareTo, which takes an object as a parameter and returns an integer. This interface gives us a way to compare one object to another. One object calls the method and passes another object as a parameter.

14 The List Interface The List interface represents an ordered collection of elements The size method returns the number of elements in the list The add method adds an element to the list The iterator and listIterator methods return iterators of the elements in the list The list interface is a popular interface in Java because it is used by many complex data structures. A the name suggests, it defines methods for handling items that are stored in some kind of list. Such as ArrayList, LinkedList and Vector. Each od these complex data structures has its own way of performing actions like getting, setting, adding, and removing items from a list. Therefore, the List interface defines these methods. But does not include the code for carrying out the actions. That is the responsibility of the classes (or subclasses) that implements the interface. The list interface is implemented by classes that represent an ordered collection of elements such as numbers or strings. ArrayList class is the only class that implements the List interface that you are required to know on the AP test.

15 Iterator and ListIterator Interfaces
The Iterator and ListIterator interfaces provide a means of moving through a collection of objects, one at a time The hasNext method returns a boolean result (true if there are items left to process) The next method returns the next object in the iteration The remove method removes the object most recently returned by the next method The ListIterator interface has additional methods (add and set) that insert or replace an element in the list


Download ppt "Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail,"

Similar presentations


Ads by Google