0% found this document useful (0 votes)
2 views4 pages

Java Packages Interfaces

The document explains the concepts of packages and interfaces in Java, detailing how to group related classes, access modifiers, and the process of importing packages. It also covers the definition, implementation, and extension of interfaces with examples. Key points include the use of keywords like 'package', 'import', 'interface', and 'implements' in Java programming.

Uploaded by

singhpratyaya999
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)
2 views4 pages

Java Packages Interfaces

The document explains the concepts of packages and interfaces in Java, detailing how to group related classes, access modifiers, and the process of importing packages. It also covers the definition, implementation, and extension of interfaces with examples. Key points include the use of keywords like 'package', 'import', 'interface', and 'implements' in Java programming.

Uploaded by

singhpratyaya999
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/ 4

Packages and Interfaces in Java

1. Packages:

A package in Java is a way to group related classes and interfaces together.

Example:

package MyPackage;

public class MyClass {

public void display() {

System.out.println("Inside MyClass from MyPackage");

2. Access Protection:

Access modifiers in Java:

- public: accessible everywhere

- protected: same package or subclasses

- (default): same package

- private: only in the same class

Example:

class Demo {

private int x = 10;

public void show() {

System.out.println("x = " + x);

}
3. Importing Packages:

To use a class from another package, use 'import'.

Example:

import MyPackage.MyClass;

public class Test {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.display();

4. Interfaces:

An interface is a collection of abstract methods.

Example:

interface Animal {

void makeSound();

5. Defining Interfaces:

Use the 'interface' keyword.

Example:

interface Vehicle {

void start();

}
6. Implementing Interfaces:

A class uses 'implements' to use an interface.

Example:

class Car implements Vehicle {

public void start() {

System.out.println("Car is starting...");

7. Applying Interfaces:

Use implemented interfaces in your program.

Example:

public class Main {

public static void main(String[] args) {

Vehicle v = new Car();

v.start();

8. Extending Interfaces:

An interface can extend another interface.

Example:

interface Engine {

void run();

interface AdvancedEngine extends Engine {


void turboBoost();

class SportsCar implements AdvancedEngine {

public void run() {

System.out.println("Running...");

public void turboBoost() {

System.out.println("Turbo Boost Activated!");

You might also like