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

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

encapsulation and abstraction
encapsulation and abstractionencapsulation and abstraction
encapsulation and abstraction
ALIZAPARVIN
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
Nikhil Agrawal
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
Sujit Kumar
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
Synapseindiappsdevelopment
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Ch2
Ch2Ch2
Ch2
Collin Aviles
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
07 intro2 oop
07 intro2 oop07 intro2 oop
07 intro2 oop
Daiva Adisurya
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
java
javajava
java
jent46
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
ale8819
 
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
CS8392-OOPS-Printed-Notes-All-Units.pdf for studentsCS8392-OOPS-Printed-Notes-All-Units.pdf for students
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
 
classes-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptxclasses-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]
Palak Sanghani
 
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
UNIT I 	OOP AND JAVA FUNDAMENTALS CONSTRUCTORUNIT I 	OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 
encapsulation and abstraction
encapsulation and abstractionencapsulation and abstraction
encapsulation and abstraction
ALIZAPARVIN
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
Sujit Kumar
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
Synapseindiappsdevelopment
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
ale8819
 
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
CS8392-OOPS-Printed-Notes-All-Units.pdf for studentsCS8392-OOPS-Printed-Notes-All-Units.pdf for students
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
 
classes-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptxclasses-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]
Palak Sanghani
 
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
UNIT I 	OOP AND JAVA FUNDAMENTALS CONSTRUCTORUNIT I 	OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 

Recently uploaded (20)

14 Years of Developing nCine - An Open Source 2D Game Framework
14 Years of Developing nCine - An Open Source 2D Game Framework14 Years of Developing nCine - An Open Source 2D Game Framework
14 Years of Developing nCine - An Open Source 2D Game Framework
Angelo Theodorou
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
COBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM CertificateCOBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM Certificate
VICTOR MAESTRE RAMIREZ
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right WayMigrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
14 Years of Developing nCine - An Open Source 2D Game Framework
14 Years of Developing nCine - An Open Source 2D Game Framework14 Years of Developing nCine - An Open Source 2D Game Framework
14 Years of Developing nCine - An Open Source 2D Game Framework
Angelo Theodorou
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
COBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM CertificateCOBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM Certificate
VICTOR MAESTRE RAMIREZ
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
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