Interface in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    Interface in java

    Hello,

    Please help me to solve this error

    I have create one interface. and then I have declare one method in it.

    Then I have implement the interface and define the method in another class.

    After that I have call the method from main.

    My program is as follows :

    Code:
    interface minte
    {
    	void prnt();
    }
    class abs implements minte
    {
    	void prnt()
    	{
    		System.out.print("From the Class abc");
    	}
    }
    class inter1
    {
    	public static void main(String args[])
    	{
    		abs x=new abs();
    		x.prnt();
    	}
    }
    But there is an error occurred

    Error :

    Code:
    21.java:7: prnt() in abs cannot implement prnt() in minte; attempting to assign
    weaker access privileges; was public
            void prnt()
                 ^
    1 error
    My file name is 21.java

    Please help me to solve this error.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    If you define methods in an interface, they must be all public, else a compile error happens. That is why your method "prnt" in line 3 is public by default, even if you do not write "public" in front of it.
    In opposite to that, methods defined in classes are not public by default. So to fix the error, you have to declare your implementing method "prn" from the interface as public: (line 7):
    Code:
        public void prnt()
    By the way:
    in your theads you always mention your filename (like "21.java"). But it has nothing to do with the problem, so you can skip that.
    On the other side, it is better to follow coding standards and only put one class in each file. Then name your file after your class. Just remember from your previous forum entry: Class and filename both must start with a capital letter. So in your case it is "Abs.java" and "Inter1.jav a")

    Comment

    • Ajay Bhalala
      New Member
      • Nov 2014
      • 119

      #3
      Hello,

      Thanks for solving my error.

      You are absolutely right,
      Now I will remember to give the filename same as the classname as you suggest.

      Your all suggestions and information are very useful for me and every beginners...

      Thanks for providing this very useful information with the solution of my problem.

      Comment

      Working...