Lecture 3
Lecture 3
void drive() {}
void brake() {}
}
What is Class?
4
The values of the variables constitute its state. The methods constitute
its behaviors.
The car has attributes, such as weight and color, and methods, such as
drive and brake.
Syntax Code
class Car
class <class_name>
{
{
int weight;
field;
String color;
method;
}
void drive() {}
void brake() {}
}
Example of classes with UML diagram
8
Example of classes with code
9
Circle c1;
Construct the instance (i.e., allocate storage for the instance and
initialize the instance) using the "new" operator.
c1 = new Circle();
Creating instance of class
21
Circle c1, c2, c3; // They hold a special value called null
}
Access specifiers in Java
24
As the name suggests access specifiers in Java helps to restrict the scope of
a class, constructor , variable , method or data member.
When you don't set access specifier for the element, it will
follow the default accessibility level.
Private methods and fields can only be accessed within public class Demo
{ // public class
the same class to which the methods and fields belong.
// private (encapsulated) instance variables
private double x, y;
Private methods and fields are not visible within public set(int a, int b)
subclasses and are not inherited by subclasses. { // setting values of private fields
x = a;
So, the private access specifier is opposite to the public y = b;
access specifier. }
public get()
Using Private Specifier we can achieve encapsulation and { // setting values of private fields
return Point(x, y);
hide data from the outside world.
}
}
Protected specifier discuss later
Real-time example and access modifiers chart
29
References
30
• https://www.javaguides.net/2019/08/oops-concepts-in-java-with
-realtime-examples.html
• https://www.javatpoint.com/object-and-class-in-java
• https://www3.ntu.edu.sg/home/ehchua/programming/java/J3a_
OOPBasics.html
• https://study.com/academy/lesson/java-data-types-object.html
• https://java-answers.blogspot.com/2012/01/access-specifiers-in-j
ava.html
31
THANK YOU
32
Next
Constructors and its types