Object Oriented Programming
Object Oriented Programming
Inheritance
Encapsulation
Abstraction
Classes
Objects
Instance
Method
Message Parsing
Class - A class can be defined as a template/blue print that
describes the behaviors/states that object of its type support.
Object - An object is an instance of a class.
Objects have states and behaviors.
Example: A dog has states - color, name, breed as well as
behaviors -wagging, barking, and eating.
Objects in Java:
Let us now look deep into what are objects.
• If you compare the software object with a real
world object, they have very similar characteristics.
• Software objects also have a state and behavior.
• A software object's state is stored in fields and
behavior is shown via methods.
• So in software development, methods operate on
the internal state of an object and the object-to-
object communication is done via methods.
2.1. Defining a Class in Java
• Java provides a reserved keyword class to define a class.
• The keyword must be followed by the class name.
• Inside the class, we declare methods and variables.
A sample of a class is given below:
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}}
• In the above example, barking(), hungry() and sleeping() are
• A class can contain any of the following variable
types.
Local variables: Variables defined inside methods,
constructors or blocks are called local variables.
• The variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.
Instance variables: Instance variables are variables
within a class but outside any method.
• These variables are instantiated when the class is
loaded.
• Instance variables can be accessed from inside any
method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared
with in a class, outside any method, with the static
2.2. Creating an Object
• basically an object is created from a class.
• In Java, the new key word is used to create new
objects.
• There are three steps when creating an object from
a class:
• Declaration: A variable declaration with a variable
name with an object type.
• Instantiation: The 'new' key word is used to create
the object.\\create an instance
• Initialization: The 'new' keyword is followed by a
call to a constructor. \\ set its initial value
• This call initializes the new object.
Example of creating an object is given below:
public class Puppy{
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args){
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
If we compile and run the above program, then it would
produce the following result:
Passed Name is :tommy
Declaring a Variable to Refer to an Object
Previously, you learned that to declare a variable,
you write:
type name;
This notifies the compiler that you will use name to
refer to data whose type is type.
With a primitive variable, this declaration also
reserves the proper amount of memory for the
variable.
You can also declare a reference variable on its own
line.
For example:
Point originOne;
• If you declare originOne like this, its value will be
undetermined until an object is actually created and
assigned to it.
• Simply declaring a reference variable does not create
an object.
• For that, you need to use the new operator.
• You must assign an object to originOne before you
use it in your code. Otherwise, you will get a compiler
error.
• A variable in this state, which currently references no
object, can be illustrated as follows (the variable
name, originOne, plus a reference pointing to
nothing):
Instantiating a Class
• The new operator instantiates a class by allocating
memory for a new object and returning a reference
to that memory.
• The new operator also invokes the object
constructor.
• Note: The phrase "instantiating a class" means the
same thing as "creating an object."
• When you create an object, you are creating an
"instance" of a class, therefore "instantiating" a
class.
• The new operator requires a single, postfix
argument: a call to a constructor.
• The name of the constructor provides the name of
the class to instantiate.
• The new operator returns a reference to the object
it created.
• This reference is usually assigned to a variable of
the appropriate type, like:
Point originOne = new Point(23, 94);
• The reference returned by the new operator does
not have to be assigned to a variable.
Initializing an Object
Here's the code for the Point class:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
• This class contains a single constructor.
• You can recognize a constructor because its
declaration uses the same name as the class and it
has no return type.
• The constructor in the Point class takes two integer
arguments, as declared by the code (int a, int b).
• The following statement provides 23 and 94 as
values for those arguments:
Point originOne = new Point(23, 94);
Here's the code for the Rectangle class, which contains
four constructors:
public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the
rectangle
public int getArea() {
return width * height;
}
}
• Each constructor lets you provide initial values for
the rectangle's origin, width, and height, using both
primitive and reference types.
• If a class has multiple constructors, they must have
different signatures.
• The Java compiler differentiates the constructors
based on the number and the type of the
arguments.
• All classes have at least one constructor.
• If a class does not explicitly declare any, the Java
compiler automatically provides a no-argument
constructor, called the default constructor.
Accessing Instance Variables and Methods
• Instance variables and methods are accessed via
created objects.
• To access an instance variable the fully qualified path
should be as follows:
/* First create an object */
ObjectReference = new Constructor();