Java Preparation Unit - 3
Java Preparation Unit - 3
Unit- 3
Inheritance, Interfaces and Package
2 Marks
1. Give a syntax to create a package and accessing package in java.
Ans) Syntax to create a package:
package package_name;
Syntax to access a package in Java:
import package_name.*;
Syntax:
public interface InterfaceName {
// Constant declarations (optional)
// Method signatures (abstract methods)
}
Example:
interface Greeting {
void greet();
}
Must have different parameter Must have the same parameter types
Parameters types or numbers. and number, or covariant types.
interface B {
void methodB();
}
interface Pet {
void play();
}
void changeValue() {
// This will cause a compilation error
value = 20;
}
}
6 Marks
1. Define packages . How to create user defined package in Java. Explain with an
suitable example.
OR
Explain how to create a package and how to import it.
OR
Describe the package in java with suitable example.
OR
How to create user defined package in Java. Explain with an suitable example.
Ans) Package:
A package is nothing but the namespace used for organizing classes and interfaces,
providing a way to manage and structure large-scale Java applications.
Example:
Package: package1
Box.java:
package package1;