Results 1 to 14 of 14

Thread: Interface help

  1. #1
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Interface help

    Hi there... I'm teaching myself Java, and I'm trying to understand interfaces.

    Here's a bit of code I'm playing with, and I'm wondering what I'm doing wrong

    Java Code:
    public class StudentDemo
    {
    	public void main(String[] args)
    	{
    		Student stu = new Student();
    		stu.setName("wallace");
    		stu.setYearOfCommencement(1101);
    	}
    	
    	public class Student implements StudentSpecification
    	{
    		String stuName = null;
    		int stuYear = 0;
    		public void setName(String name);
    		{
    			stuName = name;
    		}
    
    		public void setYearOfCommencement(int year);	
    		{
    			stuYear = year;
    		}
    
    		public String getName()
    		{
    			return stuName;
    		}
    
    		public int getYearOfCommencement();	
    		{
    			return stuYear;
    		}
    	}
    
    interface StudentSpecification
    	{	
    		// Change the name of the student
    		void setName(String name);
    
    		// Change the year of commencement
    		void setYearOfCommencement(int year); 
    
    		// Get the name
    		String getName();
    
    		int getYearOfCommencement();
    	}
    }
    Can anyone point me in the right direction?

    I'm finding alot of information online, but the examples the use never pass arguments like mine does.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What problems are you running into? Try creating classes and interfaces as separate things, not inside the tester class.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How about prividing relevant information about what is wrong. Do you get errors? then post the EXACT error messages and indicate on which line they occur. Doe the code run but not produce correct output? Then show the output you get and what you expect instead. Don't make us guess.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Just spotted some problems. In the Student class compare the getName method to all your other methods. Notice anything amiss?

  5. #5
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Terribly sorry... When I compile, I see this:
    Java Code:
    StudentDemo.java:14: missing method body, or declare abstract
    		public void setName(String name);
    		            ^
    StudentDemo.java:16: cannot find symbol
    symbol  : variable name
    location: class StudentDemo.Student
    			stuName = name;
    			          ^
    StudentDemo.java:19: missing method body, or declare abstract
    		public void setYearOfCommencement(int year);	
    		            ^
    StudentDemo.java:21: cannot find symbol
    symbol  : variable year
    location: class StudentDemo.Student
    			stuYear = year;
    			          ^
    StudentDemo.java:29: missing method body, or declare abstract
    		public int getYearOfCommencement();	
    		           ^
    StudentDemo.java:31: return outside method
    			return stuYear;
    			^
    6 errors
    And I dont see what difference having the interface class does in this case?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    See my reply above

  7. #7
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Oh god... That was a classic monday moment!

  8. #8
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Ok... I need to have my main method static, but in doing so I run into:
    Java Code:
    non-static variable this cannot be referenced from a static context
    At line 5

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Post your latest code.

  10. #10
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    public class StudentDemo
    {
    	public static void main(String[] args)
    	{
    		Student stu = new Student();
    		stu.setName("wallace");
    		stu.setYearOfCommencement(1101);
    	}
    	
    	public class Student implements StudentSpecification
    	{
    		String stuName = null;
    		int stuYear = 0;
    		public void setName(String name)
    		{
    			stuName = name;
    		}
    
    		public void setYearOfCommencement(int year)	
    		{
    			stuYear = year;
    		}
    
    		public String getName()
    		{
    			return stuName;
    		}
    
    		public int getYearOfCommencement()	
    		{
    			return stuYear;
    		}
    	}
    
    interface StudentSpecification
    	{	
    		// Change the name of the student
    		void setName(String name);
    
    		// Change the year of commencement
    		void setYearOfCommencement(int year); 
    
    		// Get the name
    		String getName();
    
    		int getYearOfCommencement();
    	}
    }
    Returns this:
    Java Code:
    javac *.java
    StudentDemo.java:5: non-static variable this cannot be referenced from a static context
    		Student stu = new Student();
    		              ^
    1 error

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It is a rather odd error but it is caused by having Student and StudentSpecification in the same file as StudentDemo. You can either make Student static (not recommended) or place Student and StudentSpecification in their own files.

  12. #12
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks so much for your help!

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    It is a rather odd error but it is caused by having Student and StudentSpecification in the same file as StudentDemo. You can either make Student static (not recommended) or place Student and StudentSpecification in their own files.
    It's actually caused by having Student inside StudentDemo, not by them simply being in the same file. Consequently Student can only be defined with respect to an instance of StudentDemo, unless Student was to be defined as static.

    But the best idea is, as you say, to make them their own files.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    It's actually caused by having Student inside StudentDemo, not by them simply being in the same file.
    DOH!

    Of course. I did wonder why OP wasn't getting the "only one top level class can be public" error.

Similar Threads

  1. Why INTERFACE...pls tell me please....
    By rohitjava in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 10:27 AM
  2. Interface?
    By MarkWilson in forum New To Java
    Replies: 4
    Last Post: 07-11-2008, 09:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •