0% found this document useful (0 votes)
15 views14 pages

Lec 1

Classes and objects are fundamental concepts in object-oriented programming. A class defines the common attributes and behaviors of objects. It acts as a template for creating objects. An object is an instance of a class that contains its own set of properties and can receive messages to carry out its methods. Everything in object-oriented programming is represented as an object that can be manipulated. Classes provide abstraction by defining common properties and behaviors for sets of objects.

Uploaded by

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

Lec 1

Classes and objects are fundamental concepts in object-oriented programming. A class defines the common attributes and behaviors of objects. It acts as a template for creating objects. An object is an instance of a class that contains its own set of properties and can receive messages to carry out its methods. Everything in object-oriented programming is represented as an object that can be manipulated. Classes provide abstraction by defining common properties and behaviors for sets of objects.

Uploaded by

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

Classes and Objects

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

2
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.

3
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

4
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()

5
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 behavior of a particular
type of object. Objects are referred to as “instances” of a class.

6
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

You might also like