No Slide Title

Download Report

Transcript No Slide Title

Summing Up Object Oriented Design

Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering data and methods with safe and reliable boundaries.

Inheritance extending abstractions to include more detailed knowledge Polymorphism allowing flexibility in naming entities & expressing abstractions

1. Traditionally encapsulation and abstraction have been implemented by the concept of abstract data type: a representation of an entity that includes the information about it and operations that can be performed on it. In Java this is a class.

2. Encapsulating data and methods maintains the integrity of the data: ensures it is not changed unintentionally or in any other way than intended.

(This is very important in large programs worked on by many programmers.) Encapsulation provides information hiding: details of methods are not visible by a client.

3. Packages provide a high degree on encapsulation but provide a means for classes to access data fields defined in other classes in the package. The package to which a class belongs is declared at the top of the file and all classes in the package should be stored in the same directory and that should have the same name as the package. ( see p.398) Data fields with default visibility (not declared private, protected or public) are accessible by any class in the same package.

Remember: declaring data fields public allows any class to access them. Dangerous! Avoid!

The import statement allows class members (data fields and methods) to be referred to by their class names without the package name. (compare bottom p.399 with bottom p.400) Files that do not declare themselves part of a specific package are considered part of the default package. What is the problem with this?

Data fields without scope modifiers are accessible to any class also in the default class - encapsulation is lost.

A library is a collection of classes organized into a package. Can you name some we have used?

simpleIO, java.awt, java.applet, and java.lang (automated loaded - no import needed.) What does * mean?

everything for example: Import java.awt.* means everything in the awt package.

Sometimes access is needed to data fields from a class outside the package that extends a class inside the package. How can this be accomplished without declaring the data field public?

protected.

(see p.402-403 & table p.404) If you do want a class member to be public, what must the class itself be declared?

public

4. The public members of a class constitute the rules the programmer has made for accessing the class. This is called Application Programmers Interface

5. Do Objects Communicate?

Yes. Client classes send messages to support classes. Support classes are the recipients and respond to the messages. Message passing and maintaining the right level of abstraction is controlled by defining methods that are constructors, accessors, and modifiers.

Other types of methods are operators and destructors.

Destructors are methods which delete instances of a class (objects) but Java provides a garbage collector that searches for objects no longer used and returns that memory space as available for other use. ( The programmer does not have to do it.)

6. The use of classes results in modular programming which enables a problem to be divided into smaller units which can be reused.

Programmers can adapt existing solutions to be reused in new problems. This is implemented by the inheritance between a superclass and subclass.

A subclass can invoke the constructor of its superclass by placing the instruction super(argument list); as the first line in its own constructor. (see p.392 &387)

7. Polymorphism dictionary meaning: many shaped.

In Java - methods may take a variety of forms.

( Think about the getInt() method in the SimpleGUI class. Some students used only 1 argument, a string, and others used 3 arguments: a string and 2 integers. Did the method work if only the string and 1 integer was used?

Three types of polymorphism: 1. Across classes: methods that have the same name but carry out the task by a different procedure in each class. ( see p.409 computeArea() ) 2. Method overloading: methods within one class that have the same name but different procedures. Requirement: different argument lists. (The compiler identifies which of the methods with the same name is being referenced by the method signature : its name, return type, and argument list.) 3. Method overriding: a method exists in a subclass that has the same name as a method in the superclass. (The subclass version is used.)

8. Class members and instance members: static and final Each time we create an instance of a class, that object has its own data fields which usually contain values that are different from the values in the data fields of other instances of the class. These data fields are called instance data fields or instance members.

Java provides a means for data fields which will always contain the same value, when used in any situation. These are called class data fields or class members. These are shared by all instances of the class, thereby saving memory.

Class data fields or methods are created by the keyword:

static.

Can you think of an examples?

Math.PI, Color.black, ioSample.getInt() When the keyword

final

is also used, the data field becomes constant (the value in the data field may not be modified.

Remember: static (class) methods can manipulate only class data fields - not instance data fields. (see p. 419)

Notes: A class that is final cannot be extended.

A method that is final cannot be overridden.

A method that is static may be accessed either through its class name, or through a class instance.

(see chart p.424)

9. static void main() ensures that there is only one occurrence of this method and it resides in the class (not in an instance). Therefore there is always only one entry point into your program.

10. An

abstract class

is an outline of a class.

It contains method names without bodies.

All classes that extend an abstract class must define each method. Can you create an instance of an abstract class?

No.

The concept of abstract class supports good organization of classes. An abstract class would be at the highest level in the hierarchy because it contains all the data fields and methods common to all subclasses in the hierarchy. See p. 425 & p.428.

11. Does Java support multiple inheritance?

No. (a class can extend only one superclass.) However, Java provides a mechanism called an

interface

. The only members of an interface are abstract methods and final class data fields.

A class

implements

an interface (not extend it).

The advantage is that classes have the benefits of a hierarchical structure without being tied to a superclass. Often used in GUI and graphic programs. See p. 573 & 576.