Week 3 - L7-L9
Week 3 - L7-L9
Unit No:2
Unit Name: Classes and Objects
2
Module No 2: Classes and Objects
Lecture No: 7
Introduction to Class and Object
Fundamentals
Class
E.g.
class
Person
name
data members
age
gender
E.g.
class
Circle
data member
radius
circumference()
member functions
area()
Class Objects
Source : https://www.youtube.com/watch?v=-MU0_MAi3to
Lecture No: 8
Method Definition
Class Definition
class class-name {
type instance-variable1;
type instance-variable2; Fields/
//………. Data members/
type instance-variableN; instance variables
class Circle {
double r; // radius of circle
double c ,a; //area and circumferen
type.
c1 c2
null null
c1.r c2.r
circumference() circumference()
area() area()
Lecture No: 9
Access Specifier, Class scope and accessing
class members
Class scope and accessing the class members
• 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..
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