This document discusses the differences between procedural and object-oriented programming. Procedural programming focuses on functions while object-oriented programming focuses on classes and the methods within them. Object-oriented programming combines data and operations, whereas procedural programming separates them. A class defines the variables and methods that objects of that class will have. An object is an instance of a class, and has the data and behaviors defined by its class. A class can contain fields to store data, methods to manipulate the data, and nested classes and interfaces. An example Pencil class is provided with fields to store attributes and a method to set the color.
2. Procedural vs. Object-Oriented
Programming
ο¬
The unit in procedural programming is function, and unit
in object-oriented programming is class
ο¬
Procedural programming concentrates on creating
functions, while object-oriented programming starts from
isolating the classes, and then look for the methods
inside them.
ο¬
Procedural programming separates the data of the
program from the operations that manipulate the data,
while object-oriented programming focus on both of them
figure1: procedural
figure2: object-oriented
3. Concept of Class and Object
ο¬ βClassβ
refers to a blueprint. It defines the
variables and methods the objects support
ο¬ βObjectβ
is an instance of a class. Each object
has a class which defines its data and behavior
4. Class Members
ο¬A
class can have three kinds of members:
ο‘
fields: data variables which determine the status
of the class or an object
ο‘
methods: executable code of the class built from
statements. It allows us to manipulate/change the
status of an object or access the value of the data
member
ο‘
nested classes and nested interfaces
5. Sample class
class Pencil {
public String color = βredβ;
public int length;
public float diameter;
public static long nextID = 0;
public void setColor (String newColor) {
color = newColor;
}
}
6. Sample class
class Pencil {
public String color = βredβ;
public int length;
public float diameter;
public static long nextID = 0;
public void setColor (String newColor) {
color = newColor;
}
}