0% found this document useful (0 votes)
2 views

Lecture 3

The document provides an overview of fundamental concepts in Java programming, including classes, objects, attributes, methods, and access specifiers. It explains how classes serve as blueprints for creating objects, and details the characteristics and behaviors of objects. Additionally, it covers the four types of access specifiers in Java: default, public, private, and protected, which regulate access to class members.

Uploaded by

Ahmad Ishtiaq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3

The document provides an overview of fundamental concepts in Java programming, including classes, objects, attributes, methods, and access specifiers. It explains how classes serve as blueprints for creating objects, and details the characteristics and behaviors of objects. Additionally, it covers the four types of access specifiers in Java: default, public, private, and protected, which regulate access to class members.

Uploaded by

Ahmad Ishtiaq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Definition of Classes,

objects, fields, and


methods
access specifiers
(Java)
Instructor Name: Muhammad Nadeem
Department of Computer Science, COMSATS University Islamabad - Wah Campus - Pakistan
Contents
2

 What is class, object, attribute and method?


 Creating instance of class
 Access specifiers
 References
 Next lecture
What is Class?
3

A class is a programmer-defined, abstract, self-contained, reusable


software entity that mimics a real-world thing.

A class is a 3-compartment box containing the name, variables and the


methods.
class Car
{
int weight;
String color;

void drive() {}
void brake() {}
}
What is Class?
4

A class encapsulates the data structures (in variables) and algorithms


(in methods).

The values of the variables constitute its state. The methods constitute
its behaviors.

An instance is an instantiation (or realization) of a particular item of a


class.
What is Class?
5

A class is a group of objects which have common properties.

It is a user-defined template or blueprint from which objects are


created.

A class in Java is a logical entity only.

In short, a class is the specification or template of an object.


What is Class?
6

It represents the set of properties or methods that are common to all


objects of one type.

For example: Car

The car has attributes, such as weight and color, and methods, such as
drive and brake.

A class is like an object constructor, or a "blueprint" for creating


objects.
Syntax and code example
7

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

public class Circle


{
// class name
double radius; // variables public class SoccerPlayer
String color; {
// class name
double getRadius() { ...... } // methods int number; // variables
double getArea() { ...... } String name;
} int x, y;

void run() { ...... } // methods


void kickBall() { ...... }
}
What is an object?
10

Usually a person, place or thing (a noun). An object is an


instance of class.

objects stores data and provides method for accessing and


modifying this data.

Object is considered to be partitioned area of computer


memory that stores data and a set of operations that can access
the data.
What is an object?
11

Object (Various definitions)

• An object is a real-world entity.


• An object is a runtime entity.
• The object is an entity which has state, behavior and identity.
• The object is an instance of a class.
What is an object?
12

An object has three characteristics:

•State: represents the data (value) of an object.

•Behavior: represents the behavior (functionality) of an object such as


deposit, withdraw, etc.

•Identity: An object identity is typically implemented via a unique ID. The


value of the ID is not visible to the external user. However, it is used
internally by the JVM to identify each object uniquely.
What is an object?
13

An object in Java is the physical as well as a logical


entity, whereas, a class in Java is a logical entity only.

An entity that has state and behavior is known as an


object e.g., chair, bike, marker, pen, table, car, etc.

It can be physical or logical (tangible and intangible).

The example of an intangible object is the bank


account, Time, stars, etc.
Real-world examples of object
14

Dogs have state (name, color,


breed, hungry) and behavior
(barking, fetching, wagging tail).

Chair, Bike, Marker, Pen,


Table, Car, Book, Apple, Bag
etc.
For Example, Pen is an object.
Its name is Dollars; color is
white, known as its state. It is
used to write, so writing is its
behavior.
What is an attribute?
15

Characteristics of certain object (instance variable / information /


property / characteristic / field and state)

public class MyClass public class MyClass


{ {
int x = 5;
int x = 5;
} public static void main(String[] args)
{
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
What is an attribute?
16

public class MyClass public class MyClass


{ {
int x = 10; int x = 5;
public static void main(String[] args)
{
public static void main(String[] args)
MyClass myObj = new MyClass(); {
myObj.x = 25; // x is now 25 MyClass myObj1 = new MyClass(); // Object 1
System.out.println(myObj.x); MyClass myObj2 = new MyClass(); // Object 2
} System.out.println(myObj1.x);
} System.out.println(myObj2.x);
}
}
What is method?
17

Are function that manipulate the data, an action performed by an


object (a verb)
Representation of concepts with real-world example
18
Example of class and instance of class with UML diagram
19
Creating instance of class
20

To create an instance of a class, you have to:

Declare an instance identifier (instance name) of a particular class like

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

For examples, suppose that we have a class called Circle,


we can create instances of Circle as follows:

// Declare 3 instances of the class Circle, c1, c2, and c3

Circle c1, c2, c3; // They hold a special value called null

// Construct the instances via new operator


c1 = new Circle();
c2 = new Circle(2.0);
c3 = new Circle(3.0, "red");
Creating instance of class
22

// You can Declare and Construct in the same statement

Circle c4 = new Circle();


Circle c1 = new Circle();
Circle c2 = new Circle(2.0);
Circle c3 = new Circle(3.0, "red");
Example
23

// Create a Car class


public class Car
{ je c
t
b
// Create a fullThrottle() method a ro
y C ct od
public void fullThrottle() m j e
n the r ob meth
{ d s o g s) y Ca le() od
System.out.println("The car is going as fast as it can!"); etho [] ar e a m hrott eth
e m ring r e at ullT d() m
} th n(St / C e f ee
ll / h
t e sp
, ca mai ; a ll
// Create a speed() method and add a parameter a in id
o a r() // C all th
public void speed(int maxSpeed) id e m tic v ew C (); // C
I ns c sta = n le
r
{ / /
u bli y Ca Thrott 00);
p { rm ll (2
System.out.println("Max speed is: " + maxSpeed); Ca Car.fu eed
p
} my Car.s
y m
}

}
Access specifiers in Java
24

Java Access Specifiers (also known as Visibility Specifiers ) regulate access


to classes, fields and methods in Java.

These Specifiers determine whether a field or method in a class, can be


used or invoked by another method in another class or sub-class.

Access Specifiers can be used to restrict access.

Access Specifiers are an integral part of object-oriented programming.


Access specifiers/modifiers in Java
25

As the name suggests access specifiers in Java helps to restrict the scope of
a class, constructor , variable , method or data member.

There are four types of access specifiers available in java:

• Default – No keyword required


• Public
• Private
• Protected
Default specifier in Java
26

When you don't set access specifier for the element, it will
follow the default accessibility level.

There is no default specifier keyword.

Classes, variables, and methods can be default accessed.

Using default specifier we can access class, method, or field


which belongs to same package, but not from outside this
package.
class Demo
{ // public class
x, y, size; // public instance variables
}
Public specifier in Java
27

Public specifiers achieves the highest level of accessibility.

Classes, methods, and fields declared as public can be accessed


from any class in the Java program, whether these classes are
in the same package or in another package.

public class Demo


{ // public class
public x, y, size; // public instance variables
}
Private specifier in Java
28

Private Specifiers achieves the lowest level of


accessibility.

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

You might also like