100% found this document useful (1 vote)
156 views

Java - Access Modifiers

Java provides four access modifiers - default, private, public, and protected. The default access modifier allows access within the same package, private restricts access to only the class, public allows access anywhere, and protected allows access within subclasses in other packages or any class in the package. Encapsulation in Java involves making variables private and providing public getter and setter methods to access and modify private variables. This allows a class to control access to its fields. Inheritance in Java uses the extends keyword and allows a subclass to inherit fields and methods from a parent class.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
156 views

Java - Access Modifiers

Java provides four access modifiers - default, private, public, and protected. The default access modifier allows access within the same package, private restricts access to only the class, public allows access anywhere, and protected allows access within subclasses in other packages or any class in the package. Encapsulation in Java involves making variables private and providing public getter and setter methods to access and modify private variables. This allows a class to control access to its fields. Inheritance in Java uses the extends keyword and allows a subclass to inherit fields and methods from a parent class.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Java - Access Modifiers

• Java provides a number of access modifiers to set access


levels for classes, variables, methods, and constructors.
The four access levels are −
• Visible to the package, the default. No modifiers are
needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
Default Access Modifier - No Keyword
• 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.
Example: - No Keyword
Private Access Modifier - Private
• Methods, variables, and constructors that are declared
private can only be accessed within the declared class
itself.
• Private access modifier is the most restrictive access
level. Class and interfaces cannot be private.
• Variables that are declared private can be accessed
outside the class, if public getter methods are present
in the class.
• Using the private modifier is the main way that an
object encapsulates itself and hides data from the
outside world.
Private Access Modifier - Private
Public Access Modifier - Public

• 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.
Public Access Modifier - Public

• The main() method of an application has to be


public.
• Otherwise, it could not be called by a Java
interpreter (such as java) to run the class.
Protected Access Modifier – Protected
• Variables, methods, and constructors, which are declared
protected in a super-class 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.
Protected Access Modifier – Protected
Access Control and Inheritance

• The following rules for inherited methods are


enforced −
• Methods declared public in a super-class also must be
public in all subclasses.
• Methods declared protected in a super-class 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.
Understanding all java access modifiers
Encapsulation
• Encapsulation is one of the four fundamental OOP concepts.
The other three are inheritance, polymorphism, and
abstraction.
• Encapsulation in Java is a mechanism of wrapping the data
(variables) and code acting on the data (methods) together as
a single unit. In encapsulation, the variables of a class will be
hidden from other classes, and can be accessed only through
the methods of their current class. Therefore, it is also known
as data hiding.
• To achieve encapsulation in Java −
• Declare the variables of a class as private.
• Provide public setter and getter methods to modify and view
the variables values.
Benefits of Encapsulation

• The fields of a class can be made read-only or write-


only.
• A class can have total control over what is stored in
its fields.
• The users of a class do not know how the class
stores its data. A class can change the data type of a
field and users of the class do not need to change
any of their code.
Overloading constructors
• Constructor in java is a special type of method that is used to
initialize the object.
• Java constructor is invoked at the time of object creation. It constructs
the values i.e. provides data for the object that is why it is known as
constructor.
• Rules for creating java constructor
• There are basically two rules defined for the constructor.
• Constructor name must be same as its class name
• Constructor must have no explicit return type
• Types of java constructors
• There are two types of constructors:
• Default constructor (no-arg constructor)
• Parameterized constructor
Default Constructor
• A constructor that have no parameter is known as default
constructor. Syntax of default constructor:
• <class_name>(){}
• Example of default constructor
• In this example, we are creating the no-arg constructor in the Bike
class. It will be invoked at the time of object creation.
class Bike1{
Bike1(){System.out.println("Bike is created");}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
Parameterized constructor
• A constructor that have parameters is known as
parameterized constructor.
Why use parameterized constructor?
• Parameterized constructor is used to provide different
values to the distinct objects.

Constructor Overloading
• Constructor overloading is a technique in Java in which a
class can have any number of constructors that differ in
parameter lists.
• The compiler differentiates these constructors by taking
into account the number of parameters in the list and
their type.
Copy Constructor

• There is no copy constructor in java. But, we can copy


the values of one object to another like copy
constructor in C++.
• There are many ways to copy the values of one object
into another in java. They are:
• By constructor
• By assigning the values of one object into another
• By clone() method of Object class
Inheritance
• Inheritance in java is a mechanism in which one object acquires
all the properties and behaviors of parent object.
• The idea behind inheritance in java is that you can create new
classes that are built upon existing classes.
• When you inherit from an existing class, you can reuse methods
and fields of parent class, and you can add new methods and
fields also.
• Inheritance represents the IS-A relationship, also known as
parent-child relationship.
• Why use inheritance in java
• For Method Overriding (so runtime polymorphism can be
achieved).
• For Code Reusability.
Syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
• The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is
to increase the functionality.
• In the terminology of Java, a class which is inherited is called
parent or super class and the new class is called child or
subclass.
Inheritance Example
• Programmer is the subclass and
Employee is the superclass.
• Relationship between two classes
is Programmer IS-A Employee.
• It means that Programmer is a
type of Employee.
Types of inheritance in java
• On the basis of class, there can be three types of
inheritance in java: single, multilevel and hierarchical.
• In java programming, multiple and hybrid inheritance is
supported through interface only.
Types of inheritance in java
• On the basis of class, there can be three types of
inheritance in java: single, multilevel and hierarchical.
• In java programming, multiple and hybrid inheritance is
supported through interface only.

Note: Multiple inheritance is not


supported in java through class.
Q) Why multiple inheritance is not supported in java?
• To reduce the complexity and simplify the language,
multiple inheritance is not supported in java.
• Consider a scenario where A, B and C are three classes.
The C class inherits A and B classes. If A and B classes
have same method and you call it from child class object,
there will be ambiguity to call method of A or B class.
• Since compile time errors are better than runtime
errors, java renders compile time error if you inherit 2
classes. So whether you have same method or different,
there will be compile time error now.

You might also like