This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
What is Object-Oriented Programming? •A set of implementation techniques that: –can be done in any programming language –may be very difficult to do in some programming languages •A strong reflection of software engineering –abstract data types –information hiding (encapsulation)
What is Object-Oriented Programming? •A way of encouraging code reuse –produces more malleable systems •A way of keeping the programmer in touch with the problem –real world objects and actions match program objects and actions
Terms normally associated with OOP • abstraction • encapsulation • information hiding • inheritance • polymorphism
Abstraction • Functions – Write an algorithm once to be used in many situations • Objects – Group a related set of attributes and behaviors into a class • Frameworks and APIs – Large groups of objects that support a complex activity. Frameworks can be used “as is” or be modified to extend the basic behavior.
OO in Java •Language elements: > Class-based object-oriented programming language with inheritance > A class is a template that defines how an object will look and behave once instantiated •Java supports both instance and class (static) variables and methods •Nearly everything is an object •Objects are accessed via references •Object behavior can be exposed via public methods •Objects are instantiated using the new construct
Classes Classes •A class defines the characteristics of similar objects – a specification •Objects of the same class are similar with respect to: – Interface – Behavior – State space (set of possible states, and state transitions) •Classes are used to instantiate specific objects (each with
Class • most fundamental aspect of object-oriented programming • template or prototype that defines a type of object • a blueprint from which an object is actually made • describes the state or the data that each object contains • describes the behavior that each object exhibits • classes in Java support three key features of OOP: Encapsulation Inheritance
public class MyPoint { // attributes private int x; private int y; // methods public void setX(int n) { x = n; } public void setY(int n) { y = n; } public int getX() { return x; } public int getY() { return y; } public void display() { System.out.println("(" + x + "," + y + ")"); }}
• In the MyPoint class, x and y are called data members, instance variables or attributes • setX(int n), setY(int n), getX(), geY() and display() are called methods
Object • instance of a class • in order to create an instance from a class definition, use the new keyword
MyPoint p = new MyPoint(); p is a reference variable x:0
p
y:0
Point Object
Objects •An object possesses: – Identity a means of distinguishing it from other objects – State what the object remembers – Interface •Objects are: what messages the – Building blocks for object systems responds to – Identifiable abstractions – Behavior of what the object can do real world objects
Instance variables or attributes • define the state of an object in a class. • can also be objects. Methods • functions that provide processing to the object’s data. • determine the behavior of an object. • The collection of all publicly available methods is called the class interface.
Message and Object Communication •Objects communicate via messages •Interfaces define the set of valid messages that an object can receive •Messages, in Java, correspond to method calls (invocations) •Senders need a reference to the target object to send a message
Accessing State •State information can be accessed directly or by using messages •Using messages: –eliminates the dependence on implementation –allows the developer to hide the details of the underlying implementation •“Accessor” messages are usually used instead of accessing state directly –example: getSpeed() may simply access a state value called “speed” – or it could hide (or later be replaced by) a calculation to obtain the same value
Encapsulation • Hides the implementation details of a class • Forces the user to use an interface to access data • Makes the code more maintainable