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.
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