CS212 - Object
Oriented
Programming
Classes and Objects
Mehwish Kiran
[email protected]We’re going to
Objectville! We’re
leaving this dusty
ol’procedural town for
good.
Object
Oriented
Programming
• Abstraction
• Inheritance
• Polymorphism
• Encapsulation
Object Oriented concepts
• Object-oriented programming (OOP) involves programming using
objects.
• An object represents an entity in the real world that can be distinctly
identified.
• For example, a student, a desk, a circle, a button, and even a loan can
all be viewed as objects.
• An object has a unique identity, state, and behaviors.
• The state of an object consists of a set of data fields (also known as
properties) with their current values.
• The behavior of an object is defined by a set of methods.
Objects
• An object has both a state and behavior.
• state defines the object, and
• behavior defines what the object does.
Classes
• Class defines a new data type. Once defined, this
new type can be used to create objects of that
type.
• A Java class uses variables to define data fields
and methods to define behaviors.
• Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.
Classes & Objects
• A class is a template for an object, and an object
is an instance of a class.
Instance Variables
• Object’s attributes are implemented as instance
variables
• A class’s instance variables maintain data for
each object (that is, each instance) of the class.
• Instance variables are declared inside a class
declaration but outside the bodies of the class’s
methods.
Classes and Objects
Sharpen your pencil
Diff erence between Classes
and Objects
Creating Objects
Access Modifiers
• By default, the class, variable, or method can be accessed by
any class in the same package.
• Public
The class, data, or method is visible to any class in any
package.
• Private
Variables or methods declared with access modifier private
are accessible only to methods of the class in which they’re
declared
Syntax
WHY Data fields should be
private
• To protect data.
• To make class easy to maintain.
• The get and set methods are used to read and
modify private properties.
Question/
Answers