42
Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Good Questions v What are Objects? v What are Classes? v What are Messages? v What is Inheritance?

Embed Size (px)

Citation preview

Object-Oriented Programming Concepts

Good Questions

What are Objects? What are Classes? What are Messages? What is Inheritance?

What Are Objects?

Software objects model read-world objects or abstract concepts• dog, bicycle, queue

Real-world objects have states and behaviors• Dogs' states: name, color, breed, hungry• Dogs' behaviors: barking fetching

How do Software objects implement real-world objects?• Use variables to implement states• Use methods to implement behaviors

An object is a software bundle of variables and related methods

Visual Representation of A Software Object

MethodVariable

Software Bicycle

Instance Variables and Instance Method

Encapsulation

The objects' variables make up the center of the object. Methods surround and hide the object's center from

other objects. Benefit of encapsulation

• Modularity

• Information hiding

For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.

What Are Classes?

A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind.

An object is an instance of a certain class. After you have created a class, you must create an

instance of it before you can use. Class variables and class methods The benefit of Classes: Reusability

Visual Representation of A Software Class

What Are Messages?

Software objects interact and communicate with each other by sending messages to each other.

More on Messages

Components of a Message• The object to whom the message is addressed (Your

Bicycle)• The name of the method to perform(changeGears)• Any parameters needed by the method (low gear)

The Benefits of Messages• Messages passing supports all possible interactions

between objects (aside from direct variable access)• Objects don’t need to be in the same process or even on

the same machine to send and receive messages.

What is Inheritance?

Inheritance allows classes to be defined in terms of other classes• superclass and subclass

Each subclass inherits variables and methods from its superclass.

Subclasses can add variables and methods to the ones they inherit from the superclass.

Subclasses can also override inherited method and provide specialized implementations for those methods.

Inheritance or class hierarchy

Benefits of Inheritance

Programmers can reuse the code in the superclass many times.

Programmers can implement superclasses called abstract classes• Abstract class defines “generic” behaviors• Define and may partially implement the behavior but

much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

Message Example

Inheritance Example

Object-Oriented Development in Java

Agenda

Java Class and Object Declaration The Life Cycle of an Object Controlling Access to Members of a Class

Java Classes and Objects

Class: templates for specifying the state and behavior of an object at

runtime

Object: instances of a class

The concepts of class/object provide a mechanism for encapsulation

Basic Structures of a Class

Class Declaration Variable

• Instance Variable• Class Variable

Constructor Method

• Instance Method• Class Method

Cleanup Rectangle2.java

Point and Rectangle

Rectangle2.java

The Class Declaration

Constructor

A method in a class that initialize an instance of an object before it's used.• The same name as the class and have no return type

Multiple Constructors: the name name but a different number of arguments or different typed arguments• Method Overloading

Java Provides default constructors. The special variable, this, can be used inside a

method to refer to the object instance. Rectangle.java

Member Variables Declaration

Instance and Local Variable

Local variable is defined inside a block of code or a method.

Example (FirstClass.java)• Instance Variable:

firstVariable• Local Variable: half

public int getHalf() { int half; // local variable half = firstVariable / 2; return half;}

Methods

Method Declaration

Return a Value from a Method

Use return operator in the method to return the value.

Methods can return a primitive type or a reference type.

The class of the returned object must be either a subclass of or the exact class of the return type.

Method Overload

Signature of a Method: return value, name, parameter list

Method Overloading: Use the same method name with different arguments to group together related methods.

Constructors can also be overloaded.• this(parameters) : Call another constructor within a

constructor

Example: FirstClass.java, SecondClass.java, Rectangle.java

Passing Information into a Method

Argument types• primitive and reference data type: Yes• method: No

Argument Names• Can have the same name as one of the class's member

variable Use this to refer to the member variable

Primitive arguments are passed by value. Reference arguments are passed by reference. FirstClass.java

The Life Cycle of an Object

Creating Objects

Using Objects

Cleaning Up Unused Objects

Creating Objects

Rectangle r = new Rectangle(5,5,100,200);• Declaration: Rectangle r (Type name)• Instantiation: new

Allocate memory for the object Initialize instance variables Call a constructor

• Initialization by Calling a Constructor Rectangle(5,5,100,200)

Using Objects

Manipulate or inspect its variables objectReference.variable

r.x = 50 r.y = 80

Call its methods objectReference.methodName(argumentList)

r.move(20,30)

Java provides an access control mechanism whereby classes can restrict or allow access to its variables and methods.

Clean Up

When all references to an object are dropped, the object is no longer required, and become eligible for garbage collection.• Call finalize() to release system resources such as open

files or open sockets before the object is collected.• Release references to other objects• protected void finalize() throws Throwable

Rectangle2.java

Controlling Access to Members of a Class

Specifier Class Subclass Package world

private O

protected O O O

public O O O O

package O O

Private

class Alpha {

private int iamprivate;

private void privateMethod() {

System.out.println ("privateMethod");

}

}

class Beta {

void accessMethod() {

Alpha a = new Alpha();

a.iamprivate = 10;

a.privateMethod();

}

}

How About one Alpha object access the private member of another Alpha object?

Protected

package Greek;

class Alpha {

protected int iamprotected;

protected void protectedMethod() {

System.out.println ("protectedMethod");

}

}

package Greek;

class Gamma {

void accessMethod() {

Alpha a = new Alpha();

a.iamprotected = 10; a.protectedMethod();

}

}

Protected (II)

import Greek.*;package Latin;class Delta extends Alpha { void accessMethod(Alpha a, Delta d) { a.iamprotected = 10; d.iamprotected = 10; a.protectedMethod(); d.protectedMethod(); }}

Public

package Greek;

public class Alpha {

public int iampublic;

public void publicMethod() {

System.out.println("publicMethod");

}

}

import Greek.*;

package Roman;

class Beta {

void accessMethod() {

Alpha a = new Alpha();

a.iampublic = 10; a.publicMethod();

}

}

Package

package Greek;

class Alpha {

int iampackage;

void packageMethod() {

System.out.println("packageMethod");

}

}

package Greek;

class Beta {

void accessMethod() {

Alpha a = new Alpha();

a.iampackage = 10;

a.packageMethod();

}

}

Inhereitance in Java

A mechanism you can use to create a new class by extending the definition

of another class

Increase the reusability of codes

Single Inheritance

Simple Class Hierarchy

Object

Vehicle Building

Car Bike House Office

Van Truck

Superclass and Subclass

More About Inheritance

Use the extends keyword to create a subclass. Method Override

• Use method overriding when you need a subclass to replace a method of its superclass.

• Define a new method that replaces the superclass method that has the same signature.

Calling Superclass Methods• super.<method>(parameters)• super(parameters) (Calling superclass constructors)

Example: GraphicsProgram.java