0% found this document useful (0 votes)
10 views40 pages

OOP Lecture 03

The document provides an overview of classes, objects, fields, methods, and access specifiers in Java. It explains the definitions and roles of classes and objects, including how to create instances and access attributes and methods. Additionally, it discusses the four types of access specifiers in Java: default, public, private, and protected, along with their implications for data encapsulation and accessibility.

Uploaded by

sjadhfwefqw
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)
10 views40 pages

OOP Lecture 03

The document provides an overview of classes, objects, fields, methods, and access specifiers in Java. It explains the definitions and roles of classes and objects, including how to create instances and access attributes and methods. Additionally, it discusses the four types of access specifiers in Java: default, public, private, and protected, along with their implications for data encapsulation and accessibility.

Uploaded by

sjadhfwefqw
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/ 40

Definition of Classes,

objects, fields, and methods


access specifiers
(Java)

Instructor Name: Mr. Abrar Ahmad


Department of Computer Science, HITEC University Taxila - 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.

• The data, or variables, defined within a class are called instance


variables.

• The code is contained within methods.

• Collectively, the methods and variables defined within a class are


called members of the class.
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.
What is Class?
5

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 and 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.
Syntax and code example
7

Syntax Code
class Car
class <class_name>
{
{
int weight;
field;
String color;
method;
}
void drive() {}
void brake() {}

}
What is Class?
8

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


the methods.
class Car
{
int weight;
String color;

void drive() {}
void brake() {}
}
Example of classes with UML diagram
9
Example of classes with code
10

// class name
public class Circle {

// variables (attributes/ data members)


double radius;
String color;

// methods
double getRadius() { ...... }
double getArea() { ...... }
}
Example of classes with code
11

// class name
public class SoccerPlayer
{
int number; // variables
String name;
int x, y;

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


void kickBall() { ...... }
}
What is an attribute?
12

•Attributes, also known as fields or properties, represent the data associated with
objects of the class.

•They define the state of objects and store information about the objects'
characteristics.

•Attributes are declared within the class.

•Attributes are accessed using dot notation


public class MyClass
{
int x = 5;
}
What is an attribute?
13

public class MyClass


{
int x = 5;

public static void main(String[] args)


{
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
What is an attribute?
14

public class MyClass


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

Methods are function that manipulate the data, an action


performed by an object (a verb)
What is an object?
16

• Usually a person, place or thing (a noun).

• An object is an instance of class.

• Objects stores data and provides a way for accessing and


modifying this data.
What is an object?
17

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


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

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

• The example of an intangible object is the bank


account, Time, stars, etc.
What is an object?
18

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


that differentiates it from the other objects.
Real-world examples of object
20

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 Dollar; color is white, known as its
state.
It is used to write, so writing is its
behavior.
Representation of concepts with real-world example
21
Example of class and instance of class with UML
diagram
22
23
A Simple Class

• Let’s begin our study of the class with a simple example.


• Here is a class called Box that defines three instance variables:
• width,
• height,
• depth.
• Currently, Box does not contain any methods
24
A Simple Class
25
A Simple Class
main function

Object Creation

Constructor

Object is being used to


access instance variables
of the class

26
27
Adding a method to the class Box
Adding a method to the class Box
2 Objects
are being
created

Assigning values to
instance variables
using mybox1 object

Assigning values to
instance variables
using mybox2 object
Invoking method through
Invoking
28 method through mybox1 object
mybox2 object
Creating instance of class
29

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
30

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
31

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

// Create a Car class


public class Car
{
// Create a fullThrottle() method
public void fullThrottle()
{
System.out.println("The car is going as fast as it can!");
}
// Create a speed() method and add a parameter
public void speed(int maxSpeed)
{
System.out.println("Max speed is: " + maxSpeed);
}
Access specifiers in Java
33

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
34

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
35

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
36

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
37

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 with inheritance
38
package myPackage; // Main class
// First class: MyClass1 public class Main {
public class MyClass1 { public static void main(String[] args) {
public void display1() { // Creating objects of both classes
System.out.println("This is MyClass1"); MyClass1 obj1 = new MyClass1();
} MyClass2 obj2 = new MyClass2();
}
// Second class: MyClass2 // Calling methods of each class
class MyClass2 { obj1.display1();
public void display2() { obj2.display2();
System.out.println("This is MyClass2"); }
} }
Real-time example and access modifiers chart
39
References
40

• 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-
java.html
41

THANK YOU

You might also like