0% found this document useful (0 votes)
5 views

Lec 02

Uploaded by

nayyeraarif4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lec 02

Uploaded by

nayyeraarif4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented

Programming
Software-Engineering
What is OOP?

 OOP stands for Object-Oriented Programming.


 Procedural programming is about writing procedures or functions that
perform operations on the data, while object-oriented programming is about
creating objects that contain both data and functions. The programming
paradigm where everything is represented as an object is known as a truly
object-oriented programming language
 Object-oriented programming has several advantages over procedural
programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the code DRY "Don't Repeat Yourself", and makes the
code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and
shorter development time
What are Classes and Objects?

 Class is the building block of Code that leads to Object-Oriented programming is


a Class. It is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of that
class. A class is like a blueprint for an object.
 For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class
and wheels, speed limits, mileage are their properties.
 Class in Java determines how an object will behave and what the object will contain.
 A Class is a user-defined data-type which has data members and member
functions.
 Data members are the data variables and member functions are the functions used
to manipulate these variables and together these data members and member
functions define the properties and behaviour of the objects in a Class.
 In the above example of class Car, the data member will be speed limit, mileage
etc and member functions can apply brakes, increase speed etc.
What are Classes and Objects?

 An Object is an identifiable entity with some characteristics and behavior.


An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is
allocated.
 Object take up space in memory and have an associated address like a record
in pascal or structure or union in C.
What are Classes and Objects?
Data members

 Attributes and methods are basically variables and functions that


belongs to the class. These are often referred to as "class members“ or
data members
Java Class

 class is a blueprint for the object. Before we create an object, we first


need to define the class.
 We can think of the class as a sketch (prototype) of a house. It contains
all the details about the floors, doors, windows, etc. Based on these
descriptions we build the house. House is the object.
 Since many houses can be made from the same description, we can
create many objects from a class.
Java Class

 Here, fields (variables) and methods represent the state and behavior of
the object respectively.

 fields are used to store data


 methods are used to perform some operations
Creating Bye-cycle class in java
Example

In the above example, we have created a class named


Bicycle. It contains a field named gear and a method
named braking().

Here, Bicycle is a prototype. Now, we can create any


number of bicycles using the prototype. And, all the
bicycles will share the fields and methods of the
prototype.
Java Objects
 Here is how to can create an object of a class.

 We have used the new keyword along with the constructor of the
class to create an object. Constructors are similar to methods
and have the same name as the class. For example, Bicycle() is
the constructor of the Bicycle class.
 Here, sports Bicycle and touring Bicycle are the names of
objects. We can use them to access fields and methods of the
class.

 As you can see, we have created two objects of the class. We can
create multiple objects of a single class in Java.
Access Members of a Class

We can use the name of objects along with the . operator to access
members of a class. For example,
Access Members of a Class

 In the above example, we have created a class named Bicycle. It


includes a field named gear and a method named braking(). Notice the
statement

 Here, we have created an object of Bicycle named sportsBicycle. We


then use the object to access the field and method of the class.

 sportsBicycle.gear - access the field gear


 sportsBicycle.braking() - access the method braking()
Example: Java Class and Objects
Example: Java Class and Objects

 In the above program, we have created a class named Lamp. It contains a variable: isOn
and two methods: turnOn() and turnOff().

 Inside the Main class, we have created two objects: led and halogen of the Lamp class. We
then used the objects to call the methods of the class.

 led.turnOn() - It sets the isOn variable to true and prints the output.
 halogen.turnOff() - It sets the isOn variable to false and prints the output.
 The variable isOn defined inside the class is also called an instance variable. It is because
when we create an object of the class, it is called an instance of the class. And, each
instance will have its own copy of the variable.

 That is, led and halogen objects will have their own copy of the isOn variable.
Example: Create objects inside the same class

You might also like