0% found this document useful (0 votes)
6 views3 pages

Java Packages, Interfaces, Access Specifiers

The document explains packages, interfaces, and access specifiers in Java. It describes how packages group related classes, how interfaces define abstract methods for implementation, and outlines the four access levels: public, private, protected, and default. Examples are provided to illustrate user-defined packages, interface implementation, and the use of access specifiers in classes.

Uploaded by

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

Java Packages, Interfaces, Access Specifiers

The document explains packages, interfaces, and access specifiers in Java. It describes how packages group related classes, how interfaces define abstract methods for implementation, and outlines the four access levels: public, private, protected, and default. Examples are provided to illustrate user-defined packages, interface implementation, and the use of access specifiers in classes.

Uploaded by

Devi Vara Prasad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Packages, Interfaces and Access Specifiers in Java

1. Packages in Java
A package is a group of related classes and interfaces. It helps in organizing code and
avoiding name conflicts.

Example of User-Defined Package


File: MyPackage/[Link]
---------------------------------
package MyPackage;

public class Student {


public void display() {
[Link]("Student class inside MyPackage");
}
}

File: [Link]
---------------------------------
import [Link];

public class Main {


public static void main(String[] args) {
Student s = new Student();
[Link]();
}
}

2. Interfaces in Java
An interface is a collection of abstract methods and constants. A class implements an
interface using the 'implements' keyword.

Interface Example
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}

3. Access Specifiers in Java


Java provides 4 access levels: public, private, protected, and default (package-private).

Access Specifier Examples


public class Test {
public void show() {
[Link]("Public method");
}
}

class DemoPrivate {
private void display() {
[Link]("Private method");
}
public void callDisplay() {
display();
}
}

class Parent {
protected void message() {
[Link]("Protected method");
}
}

class Child extends Parent {


public void callMsg() {
message();
}
}

class DemoDefault {
void show() {
[Link]("Default method");
}
}
4. Combined Example
File: mypack/[Link]
---------------------------------
package mypack;

public class A {
public int x = 10;
protected int y = 20;
int z = 30; // default
private int w = 40;

public void show() {


[Link]("Public Access");
}
}

File: [Link]
---------------------------------
import mypack.A;

public class Main {


public static void main(String[] args) {
A obj = new A();
[Link](obj.x); // public - allowed
[Link](); // public - allowed
// obj.y; // protected - not accessible here
// obj.z; // default - not accessible
// obj.w; // private - not accessible
}
}

You might also like