Q: Illustrate the use of package, import, class and interface in java programming with
example write theory of each also for 12 marks
A:
1. Package in Java
- A package is a namespace that organizes a set of related classes and interfaces.
- Syntax: package mypackage;
2. Import in Java
- The import statement is used to bring other classes or packages into the current class.
- Syntax: import mypackage.MyClass;
3. Class in Java
- A class is a blueprint for objects. It contains fields and methods.
- Syntax: class MyClass { int x; void show() { System.out.println(x); } }
4. Interface in Java
- An interface is a reference type containing abstract methods.
- Syntax: interface MyInterface { void display(); }
Example:
package mypackage;
public interface MyInterface { void display(); }
package mypackage;
public class MyClass implements MyInterface {
public void display() { System.out.println("Display from interface"); }
public void show() { System.out.println("Show method"); }
}
import mypackage.MyClass;
public class MainApp {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
obj.show();
Q: Write java program to define package and include a class within that package
A:
// File: mypackage/MyClass.java
package mypackage;
public class MyClass {
public void displayMessage() {
System.out.println("Hello from MyClass inside mypackage!");
// File: MainApp.java
import mypackage.MyClass;
public class MainApp {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayMessage();
}
Q: Explain the concept of classpath in java and its significance in package management
A:
Classpath is a parameter that tells Java where to find .class files and packages.
Default: current directory (.)
Setting classpath:
- Temporarily: javac -cp .;mypackage MainApp.java (Windows)
- Permanently: Set CLASSPATH environment variable
Significance:
- Helps Java locate packages and classes
- Manages multi-folder or modular projects
- Required for using external libraries (JAR files)
- Promotes organized, reusable code
Q: Is it possible for an interface to extend an abstract class in Java? Explain with example
A:
No, an interface cannot extend an abstract class in Java.
Interfaces can only extend other interfaces.
Incorrect:
abstract class A {}
interface B extends A {} // Error
Correct way:
abstract class A {
void show() { System.out.println("From A"); }
interface B { void display(); }
class C extends A implements B {
public void display() { System.out.println("From B"); }
Q: Difference between classes and interfaces
A:
| Feature | Class | Interface |
|-----------------|--------------------------------|----------------------------------------|
| Definition | Blueprint to create objects | Contract defining abstract behavior |
| Methods | Can have method bodies | Abstract (default/static from Java 8) |
| Variables | Instance variables | public static final only |
| Inheritance | Single inheritance | Multiple inheritance |
| Object creation | Can be instantiated | Cannot be instantiated |
Q: How user-defined packages are created and accessed in Java? Briefly explain naming
conventions in package
A:
Creating user-defined package:
1. Use `package` keyword in the class file.
2. Save it in a folder matching the package name.
3. Use `import` to access it in other files.
Example:
package mypackage;
public class MyClass { public void display() { ... } }
import mypackage.MyClass;
Naming conventions:
- Use lowercase names: mypackage
- Use reverse domain: com.example.project
- Avoid special characters
- Use plural for utility packages: utilities
Q: Abstract class and interface look same contain method name but not implementation
A:
Similarities:
- Both can have abstract methods.
- Cannot be directly instantiated.
Differences:
| Feature | Abstract Class | Interface |
|----------------|-------------------------------|-------------------------------------|
| Methods | Can be abstract + concrete | Abstract, default, static methods |
| Variables | Instance variables allowed | public static final only |
| Constructor | Can have constructors | Cannot have constructors |
| Inheritance | Single inheritance | Multiple interfaces supported |
Use abstract class for shared code, interface for behavior contracts.