[go: up one dir, main page]

0% found this document useful (0 votes)
20 views17 pages

8.Encapsulation_and_Inheritance-2024-25

Uploaded by

Jathin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

8.Encapsulation_and_Inheritance-2024-25

Uploaded by

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

⦿ Access Modifiers: in Java, access modifiers

are used to set the accessibility (visibility) of


classes, interfaces, variables, methods,
constructors, data members.
⦿ public: which can be accessed outside the
class visibility
⦿ private: can be accessed only within the
class visibility
⦿ protected: can be accessed only within the
class visibility and in the inherited classes
⦿ default(no access specifier given ), can be
accessed only within the package.
default
 1.default:When no access modifier is specified for a class, method,
or data member – It is said to be having 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.
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.
•Any other class of the same package will not be able to access these
members.
•Top-level classes or interfaces can not be declared as private because
• private means “only visible within the enclosing class”.
• protected means “only visible within the enclosing class and any
subclasses of different packages”
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.(syntax error)
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.

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.
⦿ Encapsulation:
⦿ Process of wrapping data and functions
together as a unit , can be made possible by
making class members private.
⦿ Encapsulation supports data hiding of the
objects. Private visibility supports data
hiding.
⦿ Scope of variables refers to the extent of their
usage.
⦿ class test
⦿{
⦿ public static void main()
⦿{
⦿ int a=5,b=8;
⦿ if(a<b)
⦿{
⦿ int c=a-b;
⦿}
⦿ S.o.p(c);//error, as c is local to if block
⦿ Local Variable
⦿ A variable declared inside the body of the method is called local
variable. You can use this variable only within that method and
the other methods in the class aren't even aware that the
variable exists.
⦿ A local variable cannot be defined with "static" keyword.
⦿ 2) Instance Variable
⦿ A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared
as static.
⦿ It is called an instance variable because its value is instance-
specific and is not shared among instances.
⦿ 3) Static variable
⦿ A variable that is declared as static is called a static variable. It
cannot be local. You can create a single copy of the static
variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when
the class is loaded in the memory.
⦿ public class A
⦿{
⦿ static int m=100;//static or class variable
⦿ int data; // instance variable
⦿ void method()
⦿ {
⦿ int n=90;//local variable
⦿ }
⦿ public static void main(String args[])
⦿ {
⦿ data=50;//instance variable
⦿ }
⦿ }//end of class
⦿ A process by which some properties of a class are
shared by another class or classes is called
Inheritance, it enhances the reusability of code.
⦿ extends keyword is used in inheritance .
⦿ Public <derived class> extends <parent class or
base class>
⦿ Ex:
⦿ class xyz extends abc
⦿{
⦿}
⦿ (xyz id derived class, abc is base class)
⦿ Single
⦿ Multiple
⦿ Nested or Multi level
⦿ Hierarchical
⦿ Hybrid
class Animal1 {
// protected method in parent class
protected void display() {
System.out.println("I am an animal");
}
}
⦿ classDog extends Animal1 {
⦿ public static void main(String[] args) {

⦿ // create an object of Dog class


⦿ Dog dog = new Dog();
⦿ // access protected method
⦿ dog.display();
⦿ }
⦿}
⦿ Multiple
inheritance
not used in Java
through classes but we
can implement
multiple inheritance
through interfaces in
Java

You might also like