Sie sind auf Seite 1von 7

4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

OOPS
OBJECT ORIENTED
PROGRAMMING

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

WHAT IS OBJECT ORIENTED PROGRAMMING ? WHAT IS OBJECT ORIENTED PROGRAMMING ?


Object oriented programming involves programming using OBJECTS.
Object oriented programming involves programming using OBJECTS.

IS OBJECT ORIENTED PROGRAMMING USEFUL?


WHAT IS AN OBJECT ?
OOP allows programmers to build software that is :
Reliable An object represents an entity in the real world that can be distinctly
User Friendly identified. For example a student, a circle , a desk , a car, a person ,
Maintainable a loan can all be viewed as objects.
Well-documented
Reusable software systems
4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

WHAT MAKES UP AN OBJECT ? HOW DO WE USE OBJECTS IN OUR PROGRAMMING ?


An object has a unique:
IDENTITY We create a class.
STATE
An objects state is also known as the objects properties/attributes. An CLASS : is a template, blueprint, or contract that defines what an
objects attributes are represented by data fields and their current values.
For e.g., a circle object has a data field radius, which is the property that objects data fields/attributes and methods/actions will be.
characterizes a circle.
A class can be visualized as a three-compartment box, as illustrated:
BEHAVIOUR Name (or identity): identifies the class.
An objects behavior is also known as the actions of an object. These
actions are defined by methods. To invoke a method on an object is to ask Variables (or attribute, state, field): contains the static attributes of the class.
the object to perform an action. For e.g., a circle object may invoke the Methods (or behaviors, actions, operation): contains the dynamic behaviors of the
following methods , getArea() to return its area and getPerimeter() to return
its perimeter. class.

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

An object is an instance if a class. You can create many instances EXAMPLE OF AN OBJECTs CLASS
of a class.

Creating an instance is referred to as instantiation. OBJECT : CIRCLE


The relationship between classes and objects is similar to that
between an apple-pie recipe and apple pies:
You can make as many apple pies as you want from a single
recipe.
SHOWN ON ECLIPSE
4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING


CREATING A CLASS VISIBILITY MODIFIERS
The object is the name of the class public
public class Circle The class, data, or method is visible to any class in
Within your class : any package.
- the objects attributes/ states / data fields are represented by
private
variables
private double radius; The data or methods can be accessed only by the
- the objects actions are represented by methods
declaring class.
double getArea()
{
The get and set methods are used to read and modify
return radius * radius * Math.PI;
}
private properties.

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

ACCESSOR/ GETTER METHODS MUTATOR/SETTER METHODS


It is a method used to return the current value of an attribute/data field. It is a method used to update the value of an attribute/data field.
It is a value returning method It is a void method
Data type of the method is dependent on the data type of the attribute you Data type of the method is dependent on the data type of the attribute you
are returning. (Both the data types must be the same) are returning. (Both the data types must be the same)

e.g., e.g.,
//Retrieves the current radius for this circle //Set a new radius value for this circle
public double getRadius() public void setRadius( double newRad)
{ {
return radius; radius=newRad;
} }
4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING


CREATING A CLASS
Additionally, a class has special method called a constructor.
DEMO CIRCLE OBJECTs CLASS
// Construct a circle object
public Circle()
{
radius = 1;

SHOWN ON ECLIPSE }

//Construct a circle object


public Circle(double newRadius)
{
radius = newRadius;
}

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING


CONSTRUCTORS DEFAULT CONSTRUCTORS
A constructor is a special method used to construct an object.
Constructor with no arguments.
Constructor must have the same name as the class. A class may be defined without constructors.
Constructors DONT have a return type not even void. In that case, a public non-argument constructor with an empty body is implicitly
Constructors are invoked using the new operator when an object is defined in the class.
created. This constructor is called a default constructor, its provided automatically only if no
Constructors play the role of initializing objects. constructors are explicitly defined in the class.
public static void main(String[]args)
new Classname(); PARAMETERIZED CONSTRUCTORS
e.g., Circle c1= new Circle(); // default constructor
Circle c2=new Circle(5); // parameterized constructor
Brings in a value/values as parameters and these are assigned to the data fields
4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

ACCESSING AN OBJECTS DATA & METHODS


MAIN CLASS
After an object has been created, its data/ methods can be accessed using the
Create another class that contains the main method, this class is referred to
dot operator (.)
as the main class.
dot operator(.) also known as the object member access operator.
The main classs sole purpose is to test the object class.
- Referencing the objects data:
In order to use our object class we have to create an instance of our object objectRefVariable.data field
class.
e.g., myCircle.radius
new Classname();
Circle c1 = new Circle();
- Invoking the objects method:
a circle object is created objectRefVar.methodName(arguments)
Circle object is assigned
a reference variable e.g., myCircle.getArea()

OOPS OBJECT ORIENTED PROGRAMMING

ARRAY OF OBJECTS
DEMO THE USE OF AN ARRAY OF
If we needed to create more than one object, we could use
an Array. OBJECTs
An array of objects is actually an array of reference
variables.

Circle[] circleArray = new Circle[10]; SHOWN ON ECLIPSE


4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING


THE KEYWORD this.
THE TOSTRING()

The toString() method returns a string representation of the You can use keyword "this" to refer to this instance inside a
object. class definition
Usually you should include a toString method so that it One common use of the this keyword is reference a classs
returns a digestible string representation of the object. hidden data fields.
public String toString() Another common use of the this keyword to enable a
{
constructor to invoke another constructor of the same
return "radius " + radius ;
} class.

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING


THE KEYWORD this.
public class Circle { CLASS ABSTRACTION AND ENCAPSULATION
private double radius;
The this keyword is used to
public Circle(double radius) { reference the hidden Class abstraction means to separate class implementation from the use of
this.radius = radius; data field radius of the object
being constructed. the class.
}
Creator of the class provides a description of the class and lets the user
public Circle() { The this keyword is used to know how the class can be used.
this(1.0); invoke another constructor.
}

public double getArea() { User of the class does not need to know how the class is implemented.
return this.radius * this.radius * Math.PI; The detail of implementation is encapsulated and hidden from the user.
}
}
4/5/2017

OOPS OBJECT ORIENTED PROGRAMMING OOPS OBJECT ORIENTED PROGRAMMING

CHOOSING CLASSES ADDITIONAL RESOURCES

Many methodologies for choosing and designing classes. A very simple


one is :
JAVA : THE COMPLETE REFERENCE
Write down a description of the problem. HARRY H. CHAUDHARY
- Underline nouns; they are candidates for classes or instance INTRODUCTION TO JAVA PROGRAMMING
variables
Y DANIEL LIANG 10TH EDITION
- Underline verbs; they are candidates for methods.

OOPS OBJECT ORIENTED PROGRAMMING

ADDITIONAL RESOURCES

JAVA : THE COMPLETE REFERENCE


HARRY H. CHAUDHARY
INTRODUCTION TO JAVA PROGRAMMING
Y DANIEL LIANG 10TH EDITION

Das könnte Ihnen auch gefallen