Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

interface question..  RSS feed

 
Ahmed Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi people...and merry christmass .

i do have a question about interfaces....i was working on this code.

interface Courses
{
public void courseName();
public void term();
}

public class CoursesTest implements Courses
{
public static void main(String[] args)
{
CoursesList C = new CoursesList();
C.courseName();
C.term();
}
}

my question is : how do i dispalay this text for example
JAVA PROGRAMMING
SPRING 2004.

i have tried to put these strings in the interface but, i got an error says that an interface cannot have a body...

then i have done the following:

public class CourseTest implements Course
{
public static void main(String[] args)

{
Course C = new Course();
C.courseName();
{
System.out.println("JAVA PROGRAMMING");
}

C.term();
{
System.out.println("Fall 2004");
}

}

}

but i got an error saying : course is abstract and cannot be instantiated.
Can anyone help me to figure out whats wrong?

thank alot
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class implements an interface, then it must provide bodies for the methods in that interface. (If it doesn't, then the class itself must be declared abstract and cannot be instantiated.)

Once the methods are defined in the implementing class, then you can create an instance of that class (not the interface) and call the methods.


[ December 24, 2004: Message edited by: marc weber ]
 
Ahmed Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understand that i have to modify my code to look like this :



public interface Course
{
public void courseName();


public void term();

}


public class CourseTest implements Course

{

public void CourseName()
{

System.out.println("JAVA PROGRAMMING");
}

public void term()
{

System.out.println("FALL 2004");

}


public static void main(String[] args)

{
CourseTest C = new CourseTest();

C.courseName();
C.term();

}

}

but do i have to save the file with the interface name or the class name?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Java source file can contain no more than one top-level public class or interface definition. In other words, if your interface and class are defined in the same file, then they can't both be public.

If there is a top-level class or interface definition that is public, then the source file must share the case-sensitive name of that class or interface.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the most common practice would be to have multiple files: one for the interface and one for each class that implements the interface. Then one filename == interface name and the other filename == class name.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!