Vishwa Mittar
Vishwa Mittar

Reputation: 388

creating an interface and implement it in another package

Create an interface containing three methods, in its own package. Implement the interface in a different package. Prove that all the methods in an interface are automatically public.

This is the question above which I have got as an assignment, and I am allowed to get help from anywhere. What I have tried is:

file 01: Student.java

package student;
public interface Students
{
    void RollNo();
    void course();
    void marks();
}

file 02: MyMain.java

import student.*;
class Test1 implements Students
{
    void RollNo()
    {
        System.out.println("18CS35");
    }
    void course()
    {
        System.out.println("OOP");
    }
    void marks()
    {
        System.out.println("85");
    }
}
class MyMain
{
    public static void main(String[] args)
    {
        Test1 t = new Test1();
        t.RollNo();
        t.course();
        t.marks();
    }
}

What I did is, compiled File 01 and created a package (folder in same direcotry) When I compile file 02, I get this error.

MyMain.java:2: error: cannot access Students class Test1 implements Students ^ bad class file: .\Students.class class file contains wrong class: student.Students Please remove or make sure it appears in the correct subdirectory of the classpath.

Commands I am using to compile:

javac –d . Student.java
javac Student.java      
javac MyMain.java

Upvotes: 0

Views: 6318

Answers (3)

Bentaye
Bentaye

Reputation: 9766

Declare the interface in the student package:

package student;

public interface Student {
    void rollNo();

    void course();

    void marks();
}

then in another package, declare your MyMain class

package anotherpackage;

import student.Student;

public class MyMain {

    public static void main(String[] args) {
        Test1 t = new Test1();
        t.rollNo();
        t.course();
        t.marks();
    }
}

class Test1 implements Student {
    @Override
    public void rollNo() {
        System.out.println("18CS35");
    }

    @Override
    public void course() {
        System.out.println("OOP");
    }

    @Override
    public void marks() {
        System.out.println("85");
    }
}

  • The file is called MyMain.java and the MyMain class must be declared public inside it. This is important. You must have one and only one public class in the file with the same name as the file.

  • Test1 on the other hand is not declared public.

  • You don't have to add the @Override annotations, but it is good practice to always add them when you implement or override a method;

To get it to work:

  • stick Student.java in a directory called student
  • stick MyMain.java in a directory called anotherpackage

then compile

javac student/Student.java
javac anotherpackage/MyMain.java

and run:

java anotherpackage/MyMain

outputs:

18CS35
OOP
85

The proof:

Note that when you implement your methods, you need to declare them public, otherwise the compiler will complain that you reduce their visibility. That proves that they are considered public in the interface.

Upvotes: 2

kaya3
kaya3

Reputation: 51093

Based on the commands you're using to compile the code, it looks like you've put both Students.java and MyMain.java in the same directory. Since Students is supposed to be in a package named student, your directory structure should be like below, because Java looks for the .class files in directories according to the package names.

student/
    Students.java
    Students.class
MyMain.java
MyMain.class

Or if you have separate source and build directories:

src/
    student/
        Students.java
    MyMain.java
build/
    student/
        Students.class
    MyMain.class

All of this should be handled automatically if you use an IDE like Eclipse, Netbeans or IntelliJ IDEA.

Upvotes: 0

Nicktar
Nicktar

Reputation: 5575

Your interface's methods are public by default (interface methods are always publich). Your implementation's methods have default visibility. An implementation can't have a lower visibility than the interface or abstract method it's implementing.

Just declare your methods in the Student-class public.

Upvotes: 0

Related Questions