CMP12013
Object Oriented
Modeling &
Development
Lecture
Object Oriented Concepts I
Look around....
• Name few objects you can see.
OOP
Object Oriented Programming
• OOP is a programming paradigm based on the concept of "objects," which can encapsulate
data and behavior.
• It revolves around the idea of organizing code in a way that mirrors real-world entities,
making it easier to understand, maintain, and extend software systems.
• OOP principles are widely used in modern programming languages such as Java, C++,
Python, and others.
OOP Concepts
Class
• Definition: A class is a blueprint, or prototype, that defines the
variables and the methods common to all objects of a certain kind.
• Classes are a fundamental
concept in object-oriented
programming (OOP).
• A class is a group of objects
which have common
properties.
• It is a logical entity.
• It can't be physical. Source: https://www.scaler.com/topics/difference-between-class-and-object/
• A class in Java can contain:
o Data members / Attributes / Fields
o Methods
o Constructors
o Inner classes
class <class_name>{
Syntax: field;
• <class_name> method;
o The name that identifies the class. }
• field:
o Variables that represent the state or characteristics of objects created from the class.
• method:
o Functions or procedures that define the behavior of objects.
o Methods operate on the attributes of the class and perform specific actions.
• Constructors:
o Special methods used for initializing objects when they are created.
o Constructors have the same name as the class and may take parameters.
• Inner Classes:
o Classes defined within another class.
o Can have their own attributes and methods.
public class Employee {
Example //set of attributes which describes the Object
private String name;
private int age;
private float salary;
//set of methods which describes the behaviors
public void setName(String tName) {
name = tName;
}
public void setSalary (float tSalary) {
salary = tSalary;
}
public float getSalary () {
return salary;
}
}
Methods
• In Java, a method is like a function which is used to expose the behavior of an object.
• Advantage of Methods
o Code Reusability
o Code Optimization
Defining Methods in a class
Syntax:
access_modifier return_type methodName (parameter_list) {
// statements;
}
Example: • The return Type, Method Name, and the
public void setSalary (float tSalary) { parameter list defines the Signature of the
salary = tSalary; method.
} • It is possible to define two or more
methods with the same name
Passing Parameters to a Method
• All parameters except Object types such as instances of classes and Arrays etc. are
passed by value.
• Any modifications done within the method does not affect the original variable.
• If you require modifying the original variables, and needs to pass those to a method,
declare those as instance variables in a class and pass an object of that class to the
method.
Getters and Setters
• Getter and Setter are methods used to protect your data and make your code more
secure.
• Getter and Setter make the programmer convenient in setting and getting the value for a
particular data type.
• Getter:
o Getter returns the value (accessors), it returns the value of data type int,
String, double, float, etc.
o For the program’s convenience, the getter starts with the word “get” followed by the
variable name.
o Setter:
o While Setter sets or updates the value (mutators). It sets the value for any
variable used in a class’s programs. and starts with the word “set” followed by the
variable name.
Methods
Constructor
• A constructor is a block of codes like the method.
• These methods are used to initialize objects.
• They have the same name as the class and have no return type.
• At the time of calling constructor, memory for the object is allocated in the memory.'
• Every time an object is created using the new() keyword, at least one constructor is called.
• It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.
Note:
It is called constructor because it constructs the values at the time of object creation. It is
not necessary to write a constructor for a class. It is because java compiler creates a default
constructor if your class doesn't have any.
Rules of Creating a Constructor
• There are two rules defined for the constructor.
o Constructor name must be the same as its class name.
o A Constructor must have no explicit return type.
o A Java constructor cannot be abstract, static, final, and synchronized.
Types of Constructors
• There are two types of constructors in Java:
o Default constructor (no-arg constructor)
o Parameterized constructor
Default Constructor
public class Employee{
String name;
//constructor definition
Employee(){
name = "Amali";
}
}
Parameterized Constructor
public class Employee{
String name;
//constructor definition
Employee(string name){
name = "Amali";
}
}
Java Constructor Java Method
A constructor is used to initialize the state of A method is used to expose the behavior of
an object. an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default The method is not provided by the compiler
constructor if you don't have any constructor in any case.
in a class.
The constructor name must be same as the The method name may or may not be same
class name. as the class name.
OOP Concepts
Object
• In Object-Oriented Programming (OOP), an object is a fundamental concept that
represents a real-world entity or concept within a software system.
• An object is an instance of a class, and a class is a blueprint or template for creating
objects.
• Objects encapsulate data (attributes) and behavior (methods or functions) into a single
unit, allowing for a modular and organized way of designing and implementing software.
Real-world Object
• Objects are key to understanding object-oriented technology.
• Real-world objects: your book, your desk, your television set, your car, your institute.
• Real-world objects share two characteristics:
o Attributes (State)
o Behavior.
• For Example –
• A Car has
o state - current gear, current speed
o Behavior - changing gear, Turning Wheel, Applying brakes
Software Object
• Software objects(Instances) are conceptually similar to real-world objects:
• An object stores its attributes in fields (variables in some programming languages)
• Behavior through methods (functions in some programming languages).
• Methods operate on an object's internal state and serve as the primary mechanism for
object-to-object communication.
Creating an Object
• Objects are created by instantiating classes.
• To use a class in a program, you must first create an instance of it. Objects of a class can
be created using the new operator.
• Example:-
• Employee firstEmp = new Employee();
• An instance method can be accessed using the dot(.) operator as shown below.
• A class method can be accessed by using the class name followed by a period and then the
method name.
• Syntax:
ObjectName.method();
• Example:-
e.setSalary(5000.0f);
class Example{
String name;
Example void setName(String n){
name = n;
}
String getName() {
return name;
}
public static void main(String [] args){
Example a = new Example();
Example b = new Example();
a.setName("Amal");
b.setName("Nimal");
System.out.println("Name: " + a.getName());
System.out.println("Name: " + b.getName());
}
}
class Bicycle {
int currentGearNo;
int currentSpeed;
}
class BicycleTest
{
public static void main(String args[]) {
BicycleTestob = new BicycleTest();
Bicycle b = new Bicycle();
ob.change(b);
System.out.println(b.currentGearNo);
System.out.println(b.currentSpeed);
}
void change(Bicycle b) {
b.currentGearNo = 3;
b.currentSpeed = 100;
}
}
The Object Class
• A class called Object is defined in the java.lang package of the Java standard class
library.
• All classes are derived from the Object class.
• If a class is not explicitly defined to be the child of an existing class, it is assumed to
be the child of the Object class.
• Therefore, the Object class is the ultimate root of all class hierarchies.
• All objects are guaranteed to have a toString method via inheritance.
• Thus the println method can call toString for any object that is passed to it.