Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Java Class Object Access Modifier

A Java class is a template that defines objects and includes their properties and behaviors. A class can contain fields, methods, constructors, blocks, and nested classes/interfaces. To declare a class, an access modifier is specified along with the class keyword, class name, superclass (if any), implemented interfaces (if any), and class body within braces. Common class components include access modifiers, class name, superclass, implemented interfaces, and body.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Java Class Object Access Modifier

A Java class is a template that defines objects and includes their properties and behaviors. A class can contain fields, methods, constructors, blocks, and nested classes/interfaces. To declare a class, an access modifier is specified along with the class keyword, class name, superclass (if any), implemented interfaces (if any), and class body within braces. Common class components include access modifiers, class name, superclass, implemented interfaces, and body.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Class

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


o It is a template or blueprint from which objects are created.
o It is a logical entity.
o It can't be physical.
o A class in Java can contain:
 Data Member/Fields
 Methods
 Constructors
 Blocks
 Nested class and interface

Syntax to declare a class:


access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}

Components of Java Classes

In general, class declarations can include these components, in


order:
1. Access Modifiers: A class can be public or has default access.
2. Class keyword: The class keyword is used to create a class.
3. Class name: The name should begin with an initial letter
(capitalized by convention).
4. Superclass (if any): The name of the class’s parent (superclass),
if any, preceded by the keyword extends. A class can only
extend (subclass) one parent.
5. Interfaces (if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
6. Body: The class body is surrounded by braces, { }.

Example Program: Student.java


//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference varia
ble
System.out.println(s1.name);
}
}
Output:
0
null

JAVA Object
 An object in Java is a basic unit of Object-Oriented
Programming and represents real-life entities.
 Objects are the instances of a class that are created to use the
attributes and methods of a class.
 A typical Java program creates many objects, which as you know,
interact by invoking methods.
 An object consists of:
1. State: It is represented by attributes of an object. It also
reflects the properties of an object.
2. Behavior: It is represented by the methods of an object. It
also reflects the response of an object to other objects.
3. Identity: It gives a unique name to an object and enables one
object to interact with other objects.

Example of an object: dog

Declaring Objects (also called instantiating a class)


 When an object of a class is created, the class is said to
be instantiated.
 All the instances share the attributes and the behavior of the class.
 But the values of those attributes, i.e., the state are unique for
each object.
 A single class may have any number of instances.
 Example:

Java Object Declaration

Access Modifiers in Java

 There are two types of modifiers in Java: access


modifiers and non-access modifiers.
 The access modifiers in Java specify the accessibility or scope of a
field, method, constructor, or class.
 We can change the access level of fields, constructors, methods,
and class by applying the access modifier on it.

There are four types of Java access modifiers:


1. Private: The access level of a private modifier is only within the
class. It cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you do
not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the
package and outside the package through the child class. If you do
not make the child class, it cannot be accessed from outside the
package.
4. Public: The access level of a public modifier is everywhere. It can
be accessed from within the class, outside the class, within the
package, and outside the package.
There are many non-access modifiers, such as static, abstract,
synchronized, native, volatile, transient, etc. Here, we are going to learn
the access modifiers only.

1. Default Access Modifier


 When no access modifier is specified for a class, method, or
data member – It is said to have the default access modifier
by default.
 The data members, classes, or methods that are not
declared using any access modifiers i.e. having default
access modifiers are accessible only within the same
package.

Program 1:
// Java program to illustrate default modifier
package p1;

// Class Geek is having Default access modifier


class Geek
{
void msg()
{
System.out.println("Hello World!");
}
}

Program 2:
// Java program to illustrate error while
// using class from different package with
// default modifier
package p2;
import p1.*;

// This class is having default access modifier


class GeekNew
{
public static void main(String args[])
{
// Accessing class Geek from package p1
Geek obj = new Geek();

obj.display();
}
}

Output:
Compile time error
2. Private Access Modifier
 The private access modifier is specified using the
keyword private.
 The methods or data members declared as private are accessible
only within the class in which they are declared.
o Any other class of the same package will not be able to
access these members.
o Top-level classes or interfaces cannot be declared as private
because
 private means “only visible within the enclosing class”.
 protected means “only visible within the enclosing
class and any subclasses”
 Hence these modifiers in terms of application to classes, apply
only to nested classes and not on top-level classes
In this example, we will create two classes A and B within the same
package p1. We will declare a method in class A as private and try to
access this method from class B and see the result.

Program:
// Java program to illustrate error while
// using class from different package with
// private modifier
package p1;

class A
{
private void display()
{
System.out.println("GeeksforGeeks");
}
}
class B
{
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
obj.display();
}
}

Output:
error: display() has private access in A
obj.display();

3. Protected Access Modifier


 The protected access modifier is specified using the
keyword protected.
 The methods or data members declared as protected
are accessible within the same package or subclasses in
different packages.
 In this example, we will create two packages p1 and p2. Class A in
p1 is made public, to access it in p2. The method display in class A
is protected and class B is inherited from class A and this
protected method is then accessed by creating an object of class
B.

Program 1:
// Java program to illustrate
// protected modifier
package p1;
// Class A
public class A
{
protected void display()
{
System.out.println("GeeksforGeeks");
}
}

Program 2:
// Java program to illustrate
// protected modifier
package p2;
import p1.*; // importing all classes in package p1

// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}

Output:
GeeksforGeeks

4. Public Access modifier


The public access modifier is specified using the keyword public.
 The public access modifier has the widest scope among all
other access modifiers.
 Classes, methods, or data members that are declared as public
are accessible from everywhere in the program. There is no
restriction on the scope of public data members.

Program 1:
// Java program to illustrate
// public modifier
package p1;
public class A
{
public void display()
{
System.out.println("GeeksforGeeks");
}
}

Program 2:
package p2;
import p1.*;
class B {
public static void main(String args[])
{
A obj = new A();
obj.display();
}
}

Output:
GeeksforGeeks
Algorithm to use access modifier in Java
Here’s a basic algorithm for using access modifiers in Java:
 Define a class: Create a class that represents the object you
want to manage.
 Define instance variables: Within the class, define instance
variables that represent the data you want to manage.
 Specify an access modifier: For each instance variable, specify an
access modifier that determines the visibility of the variable. The
three main access modifiers in Java are private, protected, and
public.
 Use private for variables that should only be accessible
within the class: If you want to prevent access to a variable
from outside the class, use the private access modifier. This is
the most restrictive access modifier and provides the greatest
level of encapsulation.
 Use protected for variables that should be accessible within
the class and its subclasses: If you want to allow access to a
variable from within the class and its subclasses, use the
protected access modifier. This is less restrictive than private and
provides some level of inheritance.
 Use public for variables that should be accessible from
anywhere: If you want to allow access to a variable from
anywhere, use the public access modifier. This is the least
restrictive access modifier and provides the least amount of
encapsulation.
 Use accessor and mutator methods to manage access to the
variables: In order to access and modify the variables, use
accessor (getter) and mutator (setter) methods, even if the
variables have a public access modifier. This provides a level of
abstraction and makes your code more maintainable and
testable.
 In this example, we will create two packages and the classes in
the packages will be having the default access modifiers and we
will try to access a class from one package from a class of the
second package.

You might also like