SlideShare a Scribd company logo
Object and Classes
Object Oriented Programming
By. Ketut Agus Seputra
What is OOP?
Object means a real-world entity
such as a pen, chair, table, computer,
watch, etc.
Object-Oriented Programming is a
methodology or paradigm to design
a program using classes and objects.
It simplifies software development
and maintenance by providing some
concepts:
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
What Is Class In OOP ?
• A class is a user defined data type that we can use
in our program, and it works as a blueprint for
creating objects but it doesn't actually provide any
real content itself
• The car class may specify that the color and model
are necessary for defining a car, but it will not
actually state what a specific car’s color or model is
• Classes enable the creation of specific instances of
the system being modeled (Object).
• Atributes and methods are basically variables and
functions that belongs to the class. There are often
referred to as class members
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Example
For example, if you want to create a
class for students. In that case,
"Student" will be a class, and
student records
(like student1, student2, etc) will be
objects.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Properties of Java Classes
• A class does not take any byte of memory.
• A class is just like a real-world entity, but it is
not a real-world entity. It's a blueprint where
we specify the functionalities.
• A class contains mainly two things: Methods
and Data Members.
• A class can also be a nested class.
• Classes follow all of the rules of OOPs such as
inheritance, encapsulation, abstraction, etc.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Creating (Declaring)
a Java Class
• Run in terminal
touch Person.java
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Types of Class
Variables
• 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.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Important Points About Variables Scope
By default, a variable has default access. Default access modifier means we do not explicitly
declare an access modifier for a class, field, method, etc.
A variable or method declared without any access control modifier is available to any other
class in the same package. The fields in an interface are implicitly public static final and the
methods in an interface are by default public.
Java provides a number of access modifiers to
set access levels for classes, variables,
methods, and constructors. The four access
levels are −
default − Visible to the package. No modifiers are needed.
private − Visible to the class only.
public − Visible to the world.
protected − Visible to the package and all subclasses.
Classes: Instance Fields
• Instance variables are specific to each
instance of the class which means that
each object created from the class will
have its own copy of these variables. These
fields can be set in the following three
ways:
• If they are public, they can be set like
this instanceName.fieldName = someValue;
• They can be set by class methods.
• They can be set by the constructor method
(shown in the next exercise).
Method
• Methods are repeatable, modular
blocks of code used to accomplish
specific tasks. We have the ability
to define our own methods that
will take input, do something with
it, and return the kind of output
we want.
• Now, we’re going to learn how to
create object behavior using
methods.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Constructors
• Rules for Creating Java Constructors
• You must follow the below-given rules while creating Java constructors:
• The name of the constructors must be the same as the class name.
• Java constructors do not have a return type. Even do not use void as a return type.
• There can be multiple constructors in the same class, this concept is known as constructor
overloading.
• The access modifiers can be used with the constructors, use if you want to change the
visibility/accessibility of constructors.
• Java provides a default constructor that is invoked during the time of object creation. If you
create any type of constructor, the default constructor (provided by Java) is not invoked.
Constructor Method in
Java
• Java classes contain a constructor method
which is used to create instances of the
class.
• The constructor is named after the class. If
no constructor is defined, a default empty
constructor is used.
Types of Java Constructors
What are Java Objects?
An object is a variable of the type class, it is a basic component of an object-
oriented programming system. A class has the methods and data members
(attributes), these methods and data members are accessed through an object.
Thus, an object is an instance of a class.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Creating (Declaring) a Java Object
• Declaration − A variable declaration with a variable name with an
object type.
• Instantiation − The 'new' keyword is used to create the object.
• Initialization − The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Accessing Instance
Variables and
Methods
• Instance variables and methods are accessed
via created objects. To access an instance
variable, following is the fully qualified path −
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Java instance
Example
• Java instances are objects that are based
on classes. For example, Bob may be an
instance of the class Person.
• Every instance has access to its own set
of variables which are known as instance
fields, which are variables declared
within the scope of the instance. Values
for instance fields are assigned within
the constructor method.
Rules for using the Classes
and Objects Concepts
• There can be only one public class per source file.
• A source file can have multiple non-public classes.
• The public class name should be the name of the source file as well which should be appended
by .java at the end. For example − the class name is public class Employee{} then the source file should
be as Employee.java.
• If the class is defined inside a package, then the package statement should be the first statement in the
source file.
• If import statements are present, then they must be written between the package statement and the
class declaration. If there are no package statements, then the import statement should be the first line
in the source file.
• Import and package statements will imply to all the classes present in the source file. It is not possible
to declare different import and/or package statements to different classes in the source file.
• Classes have several access levels and there are different types of classes;
abstract classes, final classes, etc. We will be explaining about all these in the
access modifiers chapter.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Access Modifiers
• Java access modifiers are used to specify the scope of the
variables, data members, methods, classes, or constructors.
These help to restrict and secure the access (or, level of access)
of the data.
Default (No
keyword
required)
Private Protected Public
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Default Access Modifier
• Default access modifier means we
do not explicitly declare an access
modifier for a class, field, method,
etc.
• A variable or method declared
without any access control
modifier is available to any other
class in the same package. The
fields in an interface are implicitly
public static final and the methods
in an interface are by default
public.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Private Access Modifier
• Methods, variables, and constructors
that are declared private can only be
accessed within the declared class
itself.
• Using the private modifier is the main
way that an object encapsulates
itself and hides data from the outside
world.
• Using the private modifier is the
main way that an object
encapsulates itself and hides
data from the outside world.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Protected Access
Modifier
• Variables, methods, and constructors,
which are declared protected in a
superclass can be accessed only by the
subclasses in other package or any
class within the package of the
protected members' class.
• The protected access modifier cannot
be applied to class and interfaces.
Methods, fields can be declared
protected, however methods and fields
in a interface cannot be declared
protected.
• Protected access gives the subclass a
chance to use the helper method or
variable, while preventing a nonrelated
class from trying to use it.
Public Access Modifier
• A class, method, constructor,
interface, etc. declared public can be
accessed from any other class.
Therefore, fields, methods, blocks
declared inside a public class can be
accessed from any class belonging to
the Java Universe.
• However, if the public class we are
trying to access is in a different
package, then the public class still
needs to be imported. Because of
class inheritance, all public methods
and variables of a class are inherited
by its subclasses.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Access
Modifiers and
Inheritance
• Methods declared public in a superclass also must be public in
all subclasses.
• Methods declared protected in a superclass must either be
protected or public in subclasses; they cannot be private.
• Methods declared private are not inherited at all, so there is no
rule for them.
Jurusan Teknik Informatika
Universitas Pendidikan Ganesha
Thanks
Next: Inheritance

More Related Content

PPTX
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPTX
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
PPTX
DAY_1.4.pptx
ishasharma835109
 
PDF
4th_class.pdf
RumiHossain5
 
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
object oriented programming unit two ppt
isiagnel2
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
DAY_1.4.pptx
ishasharma835109
 
4th_class.pdf
RumiHossain5
 

Similar to Classes and Object Concept Object Oriented Programming in Java (20)

PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
PPTX
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
AnujaS24
 
PPTX
Software enginnnering introduction (2).pptx
parmidakhrz02
 
PPTX
Suga java training_with_footer
Sugavanam Natarajan
 
PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
nandiaditi2010
 
PPTX
Object Oriented Programming - Copy.pptxb
benholdhausiku6
 
PPTX
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
PPT
Java lec class, objects and constructors
Jan Niño Acierto
 
PDF
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
rtreduanur247
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PDF
Java unit 7
Shipra Swati
 
PPTX
Lecture 5.pptx
AshutoshTrivedi30
 
PPTX
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
PPTX
Java chapter 5
Abdii Rashid
 
PDF
Top 100 Java Interview Questions with Detailed Answers
Whizlabs
 
PDF
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
PPTX
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
AnujaS24
 
Software enginnnering introduction (2).pptx
parmidakhrz02
 
Suga java training_with_footer
Sugavanam Natarajan
 
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
nandiaditi2010
 
Object Oriented Programming - Copy.pptxb
benholdhausiku6
 
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
Java lec class, objects and constructors
Jan Niño Acierto
 
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
rtreduanur247
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Java unit 7
Shipra Swati
 
Lecture 5.pptx
AshutoshTrivedi30
 
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
Java chapter 5
Abdii Rashid
 
Top 100 Java Interview Questions with Detailed Answers
Whizlabs
 
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Ad

Recently uploaded (20)

PPTX
Audio Editing and it's techniques in computer graphics.pptx
fosterbayirinia3
 
PPTX
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
PDF
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
introduction to dart --- Section one .pptx
marknaiem92
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
Audio Editing and it's techniques in computer graphics.pptx
fosterbayirinia3
 
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
introduction to dart --- Section one .pptx
marknaiem92
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
Ad

Classes and Object Concept Object Oriented Programming in Java

  • 1. Object and Classes Object Oriented Programming By. Ketut Agus Seputra
  • 2. What is OOP? Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts: Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 3. What Is Class In OOP ? • A class is a user defined data type that we can use in our program, and it works as a blueprint for creating objects but it doesn't actually provide any real content itself • The car class may specify that the color and model are necessary for defining a car, but it will not actually state what a specific car’s color or model is • Classes enable the creation of specific instances of the system being modeled (Object). • Atributes and methods are basically variables and functions that belongs to the class. There are often referred to as class members Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 4. Example For example, if you want to create a class for students. In that case, "Student" will be a class, and student records (like student1, student2, etc) will be objects. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 5. Properties of Java Classes • A class does not take any byte of memory. • A class is just like a real-world entity, but it is not a real-world entity. It's a blueprint where we specify the functionalities. • A class contains mainly two things: Methods and Data Members. • A class can also be a nested class. • Classes follow all of the rules of OOPs such as inheritance, encapsulation, abstraction, etc. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 6. Creating (Declaring) a Java Class • Run in terminal touch Person.java Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 7. Types of Class Variables • 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. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 8. Important Points About Variables Scope By default, a variable has default access. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are − default − Visible to the package. No modifiers are needed. private − Visible to the class only. public − Visible to the world. protected − Visible to the package and all subclasses.
  • 9. Classes: Instance Fields • Instance variables are specific to each instance of the class which means that each object created from the class will have its own copy of these variables. These fields can be set in the following three ways: • If they are public, they can be set like this instanceName.fieldName = someValue; • They can be set by class methods. • They can be set by the constructor method (shown in the next exercise).
  • 10. Method • Methods are repeatable, modular blocks of code used to accomplish specific tasks. We have the ability to define our own methods that will take input, do something with it, and return the kind of output we want. • Now, we’re going to learn how to create object behavior using methods. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 11. Constructors • Rules for Creating Java Constructors • You must follow the below-given rules while creating Java constructors: • The name of the constructors must be the same as the class name. • Java constructors do not have a return type. Even do not use void as a return type. • There can be multiple constructors in the same class, this concept is known as constructor overloading. • The access modifiers can be used with the constructors, use if you want to change the visibility/accessibility of constructors. • Java provides a default constructor that is invoked during the time of object creation. If you create any type of constructor, the default constructor (provided by Java) is not invoked.
  • 12. Constructor Method in Java • Java classes contain a constructor method which is used to create instances of the class. • The constructor is named after the class. If no constructor is defined, a default empty constructor is used.
  • 13. Types of Java Constructors
  • 14. What are Java Objects? An object is a variable of the type class, it is a basic component of an object- oriented programming system. A class has the methods and data members (attributes), these methods and data members are accessed through an object. Thus, an object is an instance of a class. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 15. Creating (Declaring) a Java Object • Declaration − A variable declaration with a variable name with an object type. • Instantiation − The 'new' keyword is used to create the object. • Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 16. Accessing Instance Variables and Methods • Instance variables and methods are accessed via created objects. To access an instance variable, following is the fully qualified path − Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 17. Java instance Example • Java instances are objects that are based on classes. For example, Bob may be an instance of the class Person. • Every instance has access to its own set of variables which are known as instance fields, which are variables declared within the scope of the instance. Values for instance fields are assigned within the constructor method.
  • 18. Rules for using the Classes and Objects Concepts • There can be only one public class per source file. • A source file can have multiple non-public classes. • The public class name should be the name of the source file as well which should be appended by .java at the end. For example − the class name is public class Employee{} then the source file should be as Employee.java. • If the class is defined inside a package, then the package statement should be the first statement in the source file. • If import statements are present, then they must be written between the package statement and the class declaration. If there are no package statements, then the import statement should be the first line in the source file. • Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file. • Classes have several access levels and there are different types of classes; abstract classes, final classes, etc. We will be explaining about all these in the access modifiers chapter. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 19. Access Modifiers • Java access modifiers are used to specify the scope of the variables, data members, methods, classes, or constructors. These help to restrict and secure the access (or, level of access) of the data. Default (No keyword required) Private Protected Public Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 20. Default Access Modifier • Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. • A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 21. Private Access Modifier • Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. • Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world. • Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 22. Protected Access Modifier • Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. • The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected. • Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
  • 23. Public Access Modifier • A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. • However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. Jurusan Teknik Informatika Universitas Pendidikan Ganesha
  • 24. Access Modifiers and Inheritance • Methods declared public in a superclass also must be public in all subclasses. • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private. • Methods declared private are not inherited at all, so there is no rule for them. Jurusan Teknik Informatika Universitas Pendidikan Ganesha