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,

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Interfaces A Java interface is a collection
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Road Map Introduction to object oriented programming. Classes
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Design and UML
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Chapter 5: Enhancing Classes
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interface.
Slides by Steve Armstrong LeTourneau University Longview, TX
Interfaces.
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
Advanced Java Programming
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Java Inheritance.
Chapter 8 Inheritance.
Review: libraries and packages
Chapter 8 Inheritance Part 2.
2009 Test Key.
Object-Oriented Design Part 2
Presentation transcript:

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, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail 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.

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.

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.

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.

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

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

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.

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

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.

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.

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 }

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.

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.

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.

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