Object Oriented Programming Techniques
Object Oriented Programming Techniques
Techniques
Prepared By : Umm-e-Laila 1
Classes And Objects
A class is a programmer defined type that serves as a blue print for instances
of class.
e.g. you can have int ,float etc but can also have like motorcycle, tube light
etc
Class define behavior and data. A class has two parts
Field i.e. data specify what the class is and whatsa its properties are.
Behavior i.e. method specify what the class does.
Fields
behavior
Defining Classes:
A class definition specify a new TYPE and its implementation.
General Syntax :
<class header>{
<class body>}
in class header , the name of the class is preceded by the keyword
class. In addition the class header can specify the following
information
Scope or accessibility modifier
class Car{
String licensePlate; // “Karachi 543 A23”
double speed; //in km per hour
double maxSpeed; //in km per hour
public void SetmaxSpeed(double maxSpeed){
If(maxSpeed>0.0)
this.maxSpeed=maxSpeed;
else maxSpeed=0.0;
}
}
Getter Method:
Also known as Accessor methods use to get the values
of a field .
Example
class Car{
String licensePlate; // “Karachi 543 A23”
double speed; //in km per hour
double maxSpeed; //in km per hour
public double GetmaxSpeed(){ return this.maxSpeed;
}
}
return
A method, unless void, returns a value of the specified type to the
calling method.
return expression;
The type of the return
value or expression must
match the method’s
declared return type.
Methods of the same class that have the same name but different
numbers or types of arguments are called overloaded methods.
Use overloaded methods when they perform similar tasks:
The compiler treats overloaded methods as completely different
methods.
The compiler knows which one to call based on the number and the
types of the arguments
Overloading on Return Values
Why not also use return values in
methodOverloading?
void f() {...}
int f() {...}
Then what would this mean?
f();
The this Reference
•Every instance method has a variable with the name this ,
which refers the current object for which the method is being called.
It is used implicitly by the compiler when your method refers to an
instance variable of the class.
Example
Return this.a * this.b;
•The this reference allows an object to refer to itself.
The this reference can also be used to distinguish the parameters
of a constructor from the corresponding instance variables with
the same names
public Account (Sring name, long acctNumber, double balanc
{
this.name = name;
this.acctNumber = acctNumber;
this.balance = balance;}
Objects:
Constructing objects with new:
To instantiate an object in java use new keyword followed by a call to the class
constructor.
e.g Car c=new Car();
or Car c; // declares a reference of Car type variable
c=new Cat();
•Constructor is a method that creates a new instance of a class.
•When an object is created its instance variable are declered to there default
values.
license Plate=null
speed=0
maxSpeed=0
Car c;
null
Object Variables are References
public class Student {
public char grade;}
public Fraction ( )
{
num = 0;
denom = 1;
}
Light() {
noOfWatts = 50;
indicator = true;
location = "X"; } }
If a class defines any explicit constructors, it can
no longer rely on the implicit default constructor
to set the state of the objects
If such a class requires a default constructor, its
implementation must be provided.
Constructor Overloading
• Like methods constructors may be overloaded
class Tree {
int height;
Tree() {
System.out.println("A seedling");
height = 0;
}
Tree(int i) {
System.out.println("A new Tree, "
+ i + " feet tall");
height = i; }
}
COPY CONSTRUCTOR:
If Sphere a=b;// b is also of type a therefore a and b
points to the same obj reference. But you will not have
distinct objects.
For distinct objects use Copy constructor.
Sphere(final Sphere OldSphere)
{
radius = OldSphere.radius; // Set the radius
// Set the coordinates of the center
xCenter = OldSphere. xCenter;
yCenter = OldSphere .yCenter;
zCenter = OldSphere. zCenter;
}
Operator new
y:
public class Polynomial copy
{
... public double getValue (double u)
{
double v;
... u: 3.0
}
} u acts like a local
variable in v:
getValue
Objects as parameters
Fraction f1 = new Fraction (1, 2); f1: addr1
Fraction f2 = new Fraction (5, 17);
Fraction f3 = f1.add (f2);
f2: addr2
Car Gearbox
The name of a class includes its package, and can be quite long
edu.purdue.tech.vehicle.Car car1; -
vehicle.Car car1;
Car car1;
Import Statement