0% found this document useful (0 votes)
32 views25 pages

Week 3 - L7-L9

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)
32 views25 pages

Week 3 - L7-L9

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/ 25

Subject Name: OOP

Unit No:2
Unit Name: Classes and Objects

Faculty Name : Mr. Dayanand Dhongade


Index

Lecture 7 – Introduction to Class and Object Fundamentals 3

Lecture 8 – Method Definition 12

Lecture 9 – Access Specifier, Class scope and accessing class members 19

2
Module No 2: Classes and Objects

Lecture No: 7
Introduction to Class and Object
Fundamentals
Class

 A class can be defined as a template/blue print that describes the


behaviors/states that object of its type support.
• It is a user-defined data type.
• It is a collection of fields/variables (data members) and member
functions/methods (procedure or function) that operate on the data.

E.g.
class
Person

name
data members
age
gender

eat() member functions


sleep()

Lecture 7- Introduction to Class and Object Fundamentals


Class

E.g.

class
Circle

data member
radius

circumference()
member functions
area()

Lecture 7- Introduction to Class and Object Fundamentals


Object

• class provides the blueprints for objects. So basically an object is created


from a class.
• Objects have states and behaviors
• The state of an object is stored in fields (variables), while methods
(functions) display the object's behaviour
Class Objects

radius= 4 radius= 3 radius= 1

6 Lecture 7- Introduction to Class and Object Fundamentals


Class and Objects

Class Objects

Person John Mary Jim Mia


55 30 38 20
name
M F M F
age
gender

Class is a template representing Objects are instances of class


common behavior of objects

Lecture 7- Introduction to Class and Object Fundamentals


Class and Objects

8 Lecture 7- Introduction to Class and Object Fundamentals


Class and Objects

9 Lecture 7- Introduction to Class and Object Fundamentals


Characteristics of object

State • Represents the data of an object

Identity • It is used internally by the JVM to


identify each object uniquely.

Behavior • Represents the behavior of an object


such as deposit, withdraw, etc

10 Lecture 7- Introduction to Class and Object Fundamentals


Video on Class and Object

Source : https://www.youtube.com/watch?v=-MU0_MAi3to

11 Lecture 7- Introduction to Class and Object Fundamentals


Module No 2: Classes and Objects

Lecture No: 8
Method Definition
Class Definition

class class-name {
type instance-variable1;
type instance-variable2; Fields/
//………. Data members/
type instance-variableN; instance variables

return type methodname1(parameter-list)


{
// body of method
} methods/
return type methodname2(parameter-list) Member functions
{
// body of method
}
}

Lecture 8- Method Definition


Class Definition

A class can contain any of the following variable types.


• Local variables: Variables defined inside methods, constructors or blocks
are called local variables. The variable will be declared and initialized within
the method and the variable will be destroyed when the method has
completed.
• Instance variables: Instance variables are variables within a class but
outside any method. These variables are initialized when the class is
instantiated. Instance variables can be accessed from inside any method,
constructor or blocks of that particular class.
• Class variables: Class variables are variables declared within a class,
outside any method, with the static keyword.

Lecture 8- Method Definition


Adding Variables & Methods

class Circle {
double r; // radius of circle
double c ,a; //area and circumferen

//Methods to return circumference and area


void circumference()
{
c=2*3.14*r;
System.out.println(“Circumference =“ +c); Method
} Definition
void area()
{
double a; Method
a= 3.14 * r * r; Definition
System.out.println(“Area =“ +a);
}
}

15 Lecture 8- Method Definition


Creating objects of a class

There are three steps when creating an object from a class:

• Declaration: A variable declaration with a variable name with an object

type.

• Instantiation: The 'new' key word is used to create the object.

• Initialization: The 'new' keyword is followed by a call to a constructor.

This call initializes the new object.

Lecture 8- Method Definition


Creating objects of a class

• Declaration of Circle class has created a new data type(user-defined) and


we can define variables (objects) of that type.
Syntax:
class-name object-name;
e.g.
Circle c1; //declaring object
Circle c2;

c1 c2

null null

Points to nothing (Null Reference) Points to nothing (Null Reference)

17 Lecture 8- Method Definition


Creating objects of a class

• Objects are created dynamically using the new keyword.


Syntax:
class-name object-name;
object-name=new class-name(); or
class-name object-name=new class-name(); //combine 2
statements
e.g.
Circle c1;
c1 = new Circle() ; or
Circle c1 = new Circle(); //combine 2 statements
c1 = new Circle() ; c2= new Circle() ;

c1.r c2.r
circumference() circumference()
area() area()

Lecture 8- Method Definition


Module No 2: Classes and Objects

Lecture No: 9
Access Specifier, Class scope and accessing
class members
Class scope and accessing the class members

• In Java, scope defines where a certain variable or method is accessible in


a program.
• Variables can be defined as having one of three types of scope:
1. Class level scope (instance variables):
– any variable declared within a class is accessible by all methods in
that class.
– Depending on its access modifier (ie. public or private), it can
sometimes be accessed outside the class.
2. Method level scope (local variables):
– any variable declared within a method, arguments included, is NOT
accessible outside that method.
3. Block scope (loop variables):
– any variable declared in a loop condition is not accessible after the
loop, unless you defined it beforehand.

Lecture 9- Access Specifier, Class scope and accessing class members 20


Access Control/ Access Modifiers

• Public : The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.

• Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.

• Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.

• Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default..

Lecture 9- Access Specifier, Class scope and accessing class members 21


Accessing the class members

• Syntax is similar to C syntax of accessing data defined in a structure.

class Circle Object-Name.Variable-Name


{
Object-Name.Method-Name(parameter-list)
double r; //instance variable

void area()
{
double a; // local variable
a= 3.14 * r * r; Circle c1= new Circle();
System.out.println(“Area =“ +a);
} c1.r = 2.0 // initialize radius
c1.area(); //calling method

Lecture 9- Access Specifier, Class scope and accessing class members 22


Accessing the class members

class Circle class MyMain


{ {
public static void main(String args[])
double r; // radius of circle {
double c ,a; //area and circumference Circle c1; // create reference
c1 = new Circle(); // create object
//method definition
void circumference() c1.r = 5; // assign value to data
field
{ c=2*3.14*r;
System.out.println(“Circumference =“ +c); // invoking method
}
c1.area();
void area() c1.circumference();
{ a=3.14 * r * r;
System.out.println(“Area =“ +a); }
} }
}

Lecture 9- Access Specifier, Class scope and accessing class members 23


Method Returning Value

class Circle class MyMain


{ {
public static void main(String args[])
double r; // radius of circle {
double c ,a; //area and circumference double area, circumference;
//Methods definitions
Circle c1; // create reference
double circumference()
c1 = new Circle(); // create object
{
c=2*3.14*r; c1.r = 5; // assign value to data field
return c ;
} // invoking method
area=c1.area();
double area() circumference=c1.circumference();
{
a=3.14 * r * r; System.out.println(“Area =“ +area);
return a; System.out.println(“Circumference
} =“ +circumference);
}
}
}

Lecture 9- Access Specifier, Class scope and accessing class members 24


Thank You

You might also like