1. Trang chủ
  2. » Công Nghệ Thông Tin

OOP 02

87 192 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 87
Dung lượng 883,77 KB

Nội dung

CHAPTER Fundamentals of Object Orientation Contents • INTRODUCTION • OBJECT-ORIENTED PROGRAMMING VS NON– OBJECT-ORIENTED PROGRAMMING • CLASSES, OBJECTS, VARIABLES, AND METHODS IN JAVA • CLASS METHODS AND VARIABLES IN JAVA • INTRODUCTION TO UML CLASS DIAGRAMS NNL – Khoa Tốn Tin ĐHKHTN Contents • IMPLEMENTATION INHERITANCE • TYPES, SUBTYPES, AND INTERFACE INHERITANCE • INTERFACES VS ABSTRACT CLASSES OVERLOADING VS OVERRIDING • CONTROLLING ACCESS TO METHODS AND DATA NNL – Khoa Toán Tin ĐHKHTN INTRODUCTION In this chapter, we review: • The fundamental concepts of object-oriented (OO) programming in order to understand how such programming is different from nonobject-oriented programming • The advantages of OO programming as applied to elegant software design • Implementation inheritance and specification inheritance NNL – Khoa Tốn Tin ĐHKHTN INTRODUCTION • We also learn some basic concepts of the Unified Modeling Language (UML) class diagrams • In addition, we will mention accessibility options (e.g., public and private) of data, methods, and classes, as they apply to Java NNL – Khoa Toán Tin ĐHKHTN OOP VS NON–OOP Non OOP: • A program is usually process-oriented or data-oriented In such programs, there are typically data globally available and procedures globally available • The main program, or its subprograms, are in control and manipulate the data • The main program, through its subprograms, has all the “intelligence” or behavior in the program and the data has no intelligence • The main program and its subprograms are responsible for everything NNL – Khoa Toán Tin ĐHKHTN OOP VS NON–OOP OOP: • A program is partitioned into a set of communicating objects Each object encapsulates all the behavior and knowledge relating to one concept • When an object needs something from another object, it sends it a message to the other object, which then performs some action and possibly returns a value to the caller NNL – Khoa Toán Tin ĐHKHTN OOP VS NON–OOP • Therefore, to start up an OO program, one typically creates a few objects and start them communicating with each other Ex: Windows, menus, buttons are objects • This view of OO programming, in which objects share the work and the responsibilities, should seem familiar in that it is the way humans typically interact with each other NNL – Khoa Tốn Tin ĐHKHTN OOP VS NON–OOP Object-Oriented Languages • We call a programming language object-oriented if the language supports classes, objects, messages, inheritance, and (subtype) polymorphism • In an OO programming language, classes can be viewed as templates for objects that describe a certain type of behavior or a certain set of responsibilities and any associated data NNL – Khoa Toán Tin ĐHKHTN OOP VS NON–OOP Object-Oriented Languages • In OO programming, an object is an instance of a class An object’s associated class defines the type of data the object maintains and its behavior or responsibilities toward that data Individual objects have their own set of data (their own state) to maintain NNL – Khoa Toán Tin ĐHKHTN 10 TYPES, SUBTYPES, AND INTERFACE INHERITANCE • Subtype polymorphism is one of the most important concepts in object orientation and is one of the main things that makes OO languages so useful • The Java libraries use polymorphism extensively In fact, the Java 1.5 API includes over 1000 interfaces Everywhere one of these Java interfaces is used, polymorphism is also being used NNL – Khoa Toán Tin ĐHKHTN 73 The Value of Polymorphism • How can we “plug-and-play” objects in our program • Let us suppose you have used the java.util.LinkedList class extensively as the collection class in a large software system • Now , we would like to replace some of the uses of the LinkedList class with a different collection class such as the ArrayList class NNL – Khoa Toán Tin ĐHKHTN 74 The Value of Polymorphism • The problem is that such a replacement might require significant changes to the system • In particular, if the software was not designed with such a replacement in mind, you would have to make a lot of changes • Not only is making these changes a lot of work, but this whole process might need to be repeated again (if it is later found that ArrayLists are also insufficient) NNL – Khoa Toán Tin ĐHKHTN 75 The Value of Polymorphism • Note that both LinkedList and ArrayList, implement the List interface • Instead of declaring variables of type LinkedList to initialize a LinkedList object: LinkedList list = new LinkedList(); • We use variables of type List: List list = new LinkedList(); NNL – Khoa Toán Tin ĐHKHTN 76 The Value of Polymorphism • We could unplug LinkedList object and replace with ArrayList: List list = new ArrayList(); • Regardless of of whether the list variable refers to a LinkedList or an ArrayList, code such as: list.clear(); will execute the appropriate clear method NNL – Khoa Toán Tin ĐHKHTN 77 The Value of Polymorphism • In summary, by declaring our collection variables to be of type List then, we are making our system plug-andplay regarding collection classes NNL – Khoa Tốn Tin ĐHKHTN 78 The Value of Polymorphism • Still one problem left, if we want to change our code to use some ArrayLists instead of LinkedLists, then everywhere our code has statements of the form: List list = new LinkedList(); we need to decide whether to change them to List list = new ArrayList(); • Can we overcome that problem? NNL – Khoa Toán Tin ĐHKHTN 79 INTERFACES VS ABSTRACT CLASSES • When declaring an abstract class, it is permissible to leave some of the methods in the class unimplemented • Suppose you have an abstract class with all abstract (unimplemented) methods and no instance variables NNL – Khoa Toán Tin ĐHKHTN 80 INTERFACES VS ABSTRACT CLASSES Compare two versions: public abstract class TotallyAbstractClass { public abstract void clear(); public abstract void visit(Object o); } public interface TotalInterface { public void clear(); public void visit(Object o); } NNL – Khoa Tốn Tin ĐHKHTN 81 INTERFACES VS ABSTRACT CLASSES • How they differ? If you needed to include one of them in your design, which should you choose? • A programmer typically implements an interface to permit subtype polymorphism • In contrast, a programmer extends a class as a way both to permit subtype polymorphism and to inherit method implementations • If there are no implementations to inherit, then an interface is usually the preferable form NNL – Khoa Tốn Tin ĐHKHTN 82 DYNAMIC METHOD INVOCATION • Consider the following code: Oval oval = new FilledOval(1,2,10,20); oval.draw(g);//for some Graphics object g • Which draw method will be executed in the second line of this example? • Java, uses dynamic method invocation Dynamic method invocation means that the Java runtime environment, when asked to invoke a method on an object, looks at the actual class of the object to find the method implementation to execute NNL – Khoa Toán Tin ĐHKHTN 83 ACCESS CONTROL • When it is said that a subclass inherits all data and methods of its superclass, that doesn’t mean that the subclass can directly access all that data or those methods • Instance variables and methods can be labeled “private,” in which case the data or methods can only be accessed within that class itself NNL – Khoa Tốn Tin ĐHKHTN 84 ACCESS CONTROL • If instance variables or methods of a class C are labeled “protected”, then they can be accessed by any subclass or by any class in the same package as C • If no access label (“public,” “private,” or “protected”) is used for an instance variable or method, then it is said to have “package” access, which means that other classes in C’s package can access it NNL – Khoa Toán Tin ĐHKHTN 85 ACCESS CONTROL • When should you use protected or package access for data? A good rule of thumb is “never,” basically because it exposes too much of the internal implementation of a class • It is a good rule of thumb to think of “protected” and “package” as a form of “public.” NNL – Khoa Toán Tin ĐHKHTN 86 ACCESS CONTROL • However, there are a few situations where it does make sense to allow protected or package access to instance or class variables (When?) NNL – Khoa Toán Tin ĐHKHTN 87

Ngày đăng: 01/12/2017, 23:59

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w