Classes and Objects
Plagiarism Policy
Any assignment found 30% or more copied
from the internet will be marked 0 (ZERO).
Any assignment copied from the class
mate will also be marked 0 (ZERO).
Both for the source and the copied one.
No consideration will be made regarding
plagiarized assignments.
Attendance Policy
Any student late in class by 15 min shall be
marked absent.
Contact
Office
G46, Ground Floor, CS Dept, Academic Block
II
Email
[email protected]
Defining Abstraction
Abstraction is the process of extracting
common features from specific examples
Abstraction is a process of defining the
essential concepts while ignoring the
inessential details
Class is an abstraction of all possible objects
7
Everything is an Object
Anything that you can describe can be represented
as an object, and that representation can be
created, manipulated and destroyed to represent
how you use the real object that it models.
8
Defining an Object
An object is a self-contained entity
with attributes and behaviors
information an object must know: behavior an object must do:
identity – uniqueness methods – what it can do
attributes – structure events – what it responds to
state – current condition
9
Class as Abstraction
A class is an abstraction of
its instances. It defines all
the attributes and methods
that its instances must also
have.
Person
name
gender
age
tellGender()
tellAge()
10
Defining a Class
A Class acts as the template from which an
object is created. The class defines the
properties of the object and the methods used
to control the object's behavior.
A Class specifies the structure of data as well as
the methods which manipulate that data. Such
data and methods are contained in each
instance of the class.
A Class is a model or template that can be
instantiated to create objects with a common
definition, and therefore common properties,
operations and behavior.
A Class provides a template for defining the 11
Basic Terminology
A class defines a kind of objects:
specifies the kinds of attributes (data) an object of
the class can have.
provides methods specifying the actions an object
of the class can take.
An object is an instance of the class.
Person is a class
Alice and Bob are objects of the Person class.
A Class as an Outline
public class Point
{
public int x , y; //data , attributes , properties
//behavior
public void move(int dx, int dy) {
x += dx; y += dy;
}
}
Public class Test
{
Public static void main(String[] arg)
{
Point p1 = new Point(); // (0,0)
P1.x=10;
P1.y=20;
Point p2 = new Point(); //0,0
P2.move(50,60);
}
}
------------ Person.java ------ defining Person ---------------------
public class Person
{
private String _name;
private String _iceCream;
public void setName(String newName)
{
_name = newName; // this. is optional
}
public void setIceCream(String newIceCream) { … }
public void print()
{
System.out.println(_name + “ likes “ + _IceCream); // this. optional
}
}
------------ PersonTest.java ----- using Person ------------------
public class PersonTest
{
public static void main(String[] args)
{
Person joe = new Person();
joe.setName(“Joseph”);
joe.setIceCream(“Rocky Road”);
Person mary = new Person();
mary.setName(“Mary”);
mary.setIceCream(“Chocolate Fudge”);
mary.print();
}
}
Difference between Procedural
and Object Oriented
Creating Classes (UML
Notations)
1.Circle
2.Rectangle
3.Time
4.Date
5.Distance
6.Car
7.Book
Declaring methods inside
classes
You must analyse
Function has to operate on
which data member ?
Working requires anything other
than the data of the class???
Take that as argument to the
function
END