1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter Day 21. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 20 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
10-1 An Interface is a special kind of class which defines a specification of a set of methods. provides a guarantee that any class that implements this.
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.
1 Objects and Classes Introduction zNow that some low-level programming concepts have been established, we can examine objects in more detail zLecture.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
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?
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
ISBN Chapter 12 Support for Object-Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Final and Abstract Classes
03/10/14 Inheritance-2.
Types of Programming Languages
Section 11.1 Class Variables and Methods
Interface.
Chapter 6 Methods: A Deeper Look
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Overriding Methods & Class Hierarchies
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Presentation transcript:

1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance zLecture focuses on: yabstract classes yformal Java interfaces ypackages

2 Abstract Classes zAn abstract class cannot be instantiated zIt is used in a class hierarchy to organize common features at appropriate levels zAn abstract method has no implementation, just a name and signature zAn abstract class often contains abstract methods zAny class that contains an abstract method is by definition abstract

3 Abstract Classes  The modifier abstract is used to define abstract classes and methods zThe children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them zIf a child class does not define all abstract methods of the parent, then the child is also abstract zAn abstract class is often too generic to be of use by itself

4 Abstract Classes  See Dinner.java Dinner.java Food PepperoniFranksBeans

5 Abstract Classes  See Printer.java Printer.java File Text_FileBinary_File Image_File

6 Abstract Classes  An abstract method cannot be declared as final, because it must be overridden in a child class  An abstract method cannot be declared as static, because it cannot be invoked without an implementation zAbstract classes are placeholders that help organize information and provide a base for polymorphic references

7 Interfaces zWe've used the term interface to mean the set of service methods provided by an object zThat is, the set of methods that can be invoked through an object define the way the rest of the system interacts, or interfaces, with that object zThe Java language has an interface construct that formalizes this concept zA Java interface is a collection of constants and abstract methods

8 Interfaces zA class that implements an interface must provide implementations for all of the methods defined in the interface zThis relationship is specified in the header of the class: class class-name implements interface-name { }  See Soap_Box.java Soap_Box.java

9 Interfaces zAn interface can be implemented by multiple classes zEach implementing class can provide their own unique version of the method definitions zAn interface is not a class, and cannot be used to instantiate an object zAn interface is not part of the class hierarchy zA class can be derived from a base class and may implement one or more interfaces

10 Interfaces zUnlike interface methods, interface constants require nothing special of the implementing class zConstants in an interface can be used in the implementing class as if they were declared locally zThis feature provides a convenient technique for distributing common constant values among multiple classes  See File_Protection.java File_Protection.java

11 Interfaces  An interface can be derived from another interface, using the extends reserved word zThe child interface inherits the constants and abstract methods of the parent zNote that the interface hierarchy and the class hierarchy are distinct zA class that implements the child interface must define all methods in both the parent and child

12 Interfaces zAn interface name can be used as a generic reference type name zA reference to any object of any class that implements that interface is compatible with that type  For example, if Philosopher is the name of an interface, it can be used as the type of a parameter to a method  An object of any class that implements Philosopher can be passed to that method

13 Interfaces zNote the similarities between interfaces and abstract classes zBoth define abstract methods that are given definitions by a particular class zBoth can be used as generic type names for references zHowever, a class can implement multiple interfaces, but can only be derived from one class  See Printer2.java Printer2.java

14 Interfaces zA class that implements multiple interfaces specifies all of them in its header, separated by commas zThe ability to implement multiple interfaces provides many of the features of multiple inheritance, the ability to derive one class from two or more parents zJava does not support multiple inheritance  See Readable_Files.java Readable_Files.java

15 Packages zA Java package is a collection of classes zThe classes in a package may or may not be related by inheritance zA package is used to group similar and interdependent classes together zThe Java API is composed of multiple packages  The import statement is used to assert that a particular program will use classes from a particular package

16 Packages zA programmer can define a package and add classes to it zThe package statement is used to specify that all classes defined in a file belong to a particular package zThe syntax of the package statement is: package package-name ; zIt must be located at the top of a file, and there can be only one package statement per file

17 Packages zThe classes must be organized in the directory structure such that they can be found when referenced by an import statement zThere is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced  See Simple_IO_Test.java Simple_IO_Test.java Simple IO Package Files: Reader.javaReader.java Writer.java

18 Packages zThe import statement specifies particular classes, or an entire package of classes, that can be used in that program zImport statements are not necessary; a class can always be referenced by its fully qualified name in- line  See Simple_IO_Test2.java Simple_IO_Test2.java zIf two classes from two packages have the same name and are used in the same program, they must be referenced by their fully qualified name

19 Packages zAs a rule of thumb, if you will use only one class from a package, import that class specifically  See Simple_IO_Test3.java Simple_IO_Test3.java zIf two or more classes will be used, use the * wildcard character in the import statement to provide access to all classes in the package