Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Design

Similar presentations


Presentation on theme: "Object-Oriented Design"— Presentation transcript:

1 Object-Oriented Design
Chapter 2

2 Objectives Review core concepts underlying object-oriented programming
Review how these concepts are accomplished in a Java program Discuss the use of generic types to define collection classes

3 Object-Orientation An object is a fundamental entity in a Java program. Java programs also manage primitive data. Primitive data includes common, fundamental values such as numbers and characters. An object usually represents something more sophisticated than primitive data.

4 OO Orientation An object is defined by a class.
A class can be thought of as the data type of the object. A data type is the values and operations on those values Objects manages and protects their own data, this is an example of encapsulation. To request an object to perform an action, you send the object a message

5 More OO Classes can be inherited from other classes, this is a form of reuse. Using inheritance you can create class hierarchies. Classes, objects, encapsulation, and inheritance are the primary ideas that make up the world of oo software.

6 Using Objects An object is an abstraction
An abstraction hides the precise details of how it works. In fact, those details are irrelevant from the point of view of the client Abstraction is one of the most powerful ideas of OO design

7 Class Libraries and Packages
A class library is a set of classes that supports development of programs. Java has a very rich set of class libraries. Sometimes these are called the Java APIs The classes in the library are organized into packages.

8 Packages Packages group logically related classes into a compilation unit Package names have to correspond to a directory structure on your computer Web-CAT will want your package names to be of a form like this: edu.vt.cs.cs1706.projectname I’ll probably go with cs1706.projectname

9 State and Behavior An object has a set of attributes which define the object state of being. The set of attributes an object will have will depend on context. An object representing a ball in a game will different set of attributes from a ball in an inventory system.

10 Encapsulation Objects should be encapsulated.
The rest of the program should interact with an object only through a well-defined interface. Visibility modifiers allow us to implement encapsulation in Java Public, private and protected

11 Effects of Public and Private Visibility
Variables Violates encapsulation Enforces encapsulation Methods Provides services to clients Supports other methods in the class

12 References Revisited Objects are stored as references
A reference variable that does currently point to an object is a null reference The this reference is a special reference that refers to the currently executing object An alias occurs when two object references refer to the same object. This should be avoided, but isn’t always a bad thing

13 Interfaces An interface is a collection of constants and abstract methods. Some examples are: Comparable Iterator Comparator Clonable

14 Inheritance Private data is private even in an inheritance hierarchy
A way to allow the subclass to have access to the private data of the parent class is to declare the fields as protected. A subclass can invoke methods of the parent class through the use of the super keyword.

15 Polymorphism Polymorphism is the ability for a software system to decide at runtime which method is invoked. This is sometimes called dynamic dispatch. You need an inheritance hierarchy. The subclass must overload at least one method. You can then declare an object of the super class and have it refer to the subclass and the subclass method will be invoked.

16 Generic Types A generic type allows us to define a class so that it stores, operates on, and manages objects whose types is not specified until the class is instantiated. class Box<T> { } T is a placeholder to allow us to define the class.


Download ppt "Object-Oriented Design"

Similar presentations


Ads by Google