Enhanced Class Design -- Introduction

Similar documents
Inheritance and Polymorphism

ECE 122. Engineering Problem Solving with Java

OVERRIDING. 7/11/2015 Budditha Hettige 82

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

What is Inheritance?

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

Inheritance -- Introduction

Chapter 6 Reflection

C++ Important Questions with Answers

ECE 122. Engineering Problem Solving with Java

Java Programming Training for Experienced Programmers (5 Days)

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inheritance. COMP Week 12

Chapter 4 Java Language Fundamentals

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 6 Introduction to Defining Classes

Chapter 5 Object-Oriented Programming

Inheritance and Substitution (Budd chapter 8, 10)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CSCE3193: Programming Paradigms

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Object-Oriented Programming

More on Inheritance. Interfaces & Abstract Classes

Object Orientation Fourth Story. Bok, Jong Soon

Basics of Object Oriented Programming. Visit for more.

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Objects and Classes -- Introduction

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Arrays Classes & Methods, Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

1 Shyam sir JAVA Notes

Java Object Oriented Design. CSC207 Fall 2014

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 4: Writing Classes

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

What are the characteristics of Object Oriented programming language?

Chapter 14 Abstract Classes and Interfaces

Chapter 8: Class and Method Design

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CLASSES AND OBJECTS IN JAVA

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 5. Inheritance

And Even More and More C++ Fundamentals of Computer Science

CS 251 Intermediate Programming Inheritance

Inheritance & Polymorphism

Packages & Random and Math Classes

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CPS 506 Comparative Programming Languages. Programming Language

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

Object-Oriented Design

Programming Exercise 14: Inheritance and Polymorphism

Lesson 10 BPEL Introduction

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS Information and Computer Science Department

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Computer Science 210: Data Structures

C++ Programming: Polymorphism

Objective-C: An Introduction (pt 1) Tennessee Valley Apple Developers Saturday CodeJam July 24, 2010 August 7, 2010

Cpt S 122 Data Structures. Introduction to C++ Part II

ECE 122. Engineering Problem Solving with Java

Polymorphism. Agenda

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Hospitality Industry Technology Integration Standards Glossary of Terminology

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance and Polymorphism

COMP 250 Fall inheritance Nov. 17, 2017

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS313D: ADVANCED PROGRAMMING LANGUAGE

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Chapter 10: Inheritance

0. Overview of this standard Design entities and configurations... 5

VDL a View Definition Language

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Data Structure. Recitation III

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

Object oriented programming Concepts

Object-Oriented Concepts and Design Principles

JessTab Manual. Integration of Protégé and Jess. Henrik Eriksson. Linköping University.

CS-202 Introduction to Object Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

3. Can an abstract class include both abstract methods and non-abstract methods? D

Transcription:

Enhanced Class Design -- Introduction We now examine several features of class design and organization that can improve reusability and system elegance Chapter 9 focuses on: abstract classes formal Java interfaces packages 1

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

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

Abstract Classes See Dinner.java Food Beans Franks Pepperoni 4

Abstract Classes See Printer.java File Binary_File Text_File Image_File 5

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 Abstract classes are placeholders that help organize information and provide a base for polymorphic references 6

Interfaces We've used the term interface to mean the set of service methods provided by an object That 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 The Java language has an interface construct that formalizes this concept A Java interface is a collection of constants and abstract methods 7

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

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

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

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

Interfaces An interface name can be used as a generic reference type name A 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 12

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

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

Packages A Java package is a collection of classes The classes in a package may or may not be related by inheritance A package is used to group similar and interdependent classes together The 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 15

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

Packages The classes must be organized in the directory structure such that they can be found when referenced by an import statement There is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced See Simple_IO_Test.java 17

Packages The import statement specifies particular classes, or an entire package of classes, that can be used in that program Import statements are not necessary; a class can always be referenced by its fully qualified name in-line See Simple_IO_Test2.java If 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 18

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