Lecture 14 - OOP Concepts I
Lecture 14 - OOP Concepts I
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
• It is a logical entity.
o Methods
o Constructors
o Inner classes
class <class_name>{
Syntax: field;
• <class_name> method;
• field:
o Variables that represent the state or characteristics of objects created from the class.
• method:
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:
• Advantage of Methods
o Code Reusability
o Code Optimization
Defining Methods in a class
Syntax:
access_modifier return_type methodName (parameter_list) {
// statements;
}
• 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.
• 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 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 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.
• 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:
• 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:-
• 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.
• Thus the println method can call toString for any object that is passed to it.