1

So I'm trying to create a program that uses a classes that implements an interface. The idea is the interface has the base stats for all possibles, like all people who use a university, while the classes are more specific, such as faculty. The user gives their first and last name, M Number (essentially an ID), and whether they are full or part time. I'm not trying to check if it exists already or anything complicated: just create the object, then output a line that says it was created.

Here's the code:

Interface:

package assignment1;

import java.util.Scanner;

public interface Person {
    String firstName, lastName, mNumber;

    void setName(String fName, String lName){
        firstName = fName; lastName = lName;    }

    String getName(){
        String fullName = this.firstName.concat(" " + this.lastName); 
        return fullName;    }

    void setMNumber(String mNum){
        mNumber = mNum;     }

    String getMNumber(){
        return mNumber;     }
    }

Class:

 package assignment1;

 import java.util.Scanner; 

 public class Faculty implements Person {
     void actionPerformed(java.awt.event.ActionEvent ev){   }
     String Type; 
     public Faculty(){  }

    public void defineType(String type){
        this.Type = type; 
    }

    String getType(){
        return this.Type; }

    void print(){
        System.out.println(" A new faculty " + this.getName() + 
                "M-Number: " + this.getMNumber() + " Type: " + 
                this.Type + " has been created.");
    }

    public static void main(String[] args){
        Faculty f1 = new Faculty();
        Scanner scant = new Scanner(System.in);
        String fName = scant.next(); 
        String lName = scant.next();
        f1.setName(fName, lName);
        String MNum = scant.next(); 
        f1.setMNumber(MNum);
        String T = scant.next();  
        f1.defineType(T); 
        f1.print();
    }
}

The problem comes when I try to run this. No matter what kind of input I give, or through all of my other attempts at fixing it, I get the following error after giving an input:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The type Faculty must implement the inherited abstract method Person.setName(String, String)

at assignment1.Faculty.setName(Faculty.java:5)
at assignment1.Faculty.main(Faculty.java:28)

If anyone could tell me what I'm doing wrong, explain why it's wrong, and tell me how to fix it, it would be greatly appreciated.

  • 1
    I think you might want to have a closer look at the [interfaces trail](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) and if you're using Java 8, [default methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) – MadProgrammer Sep 04 '15 at 03:33

4 Answers4

4

Interface methods cannot have implementation before Java 8. Starting in Java 8 this is possible, but only with default methods and static methods.

In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.)

You are probably looking for an abstract class which lets you have both abstract methods (methods that must be implemented by non-abstract subclasses) and normal methods that can be given implementation in the abstract class itself (subclasses have the option to override them of course).

But I'm not sure if you want to use an abstract class or interface. Going by the class names, Faculty would have Persons, but I don't see why one should extend/implement the other. They seem to be different things. You might want to reconsider your design and also read this: When should I use an interface in java?

Community
  • 1
  • 1
Zarwan
  • 5,537
  • 4
  • 30
  • 48
1

Interfaces cannot have logic in them prior to Java 8. You've implemented "setName" in your interface, but that's not legal in Java. An interface is purely a list of member object and methods.

I suspect what you want is instead an Abstract Class. Differences are outlined here http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/

If you are using java 8, just declare your method as default and it should work I believe.

CollinD
  • 7,304
  • 2
  • 22
  • 45
  • 1
    *"Interfaces cannot have logic in them"* ... actually ... [Default Methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) – MadProgrammer Sep 04 '15 at 03:32
  • 1
    Guess I'm not up to date on my Java 8. Thanks for the info. I've amended my post. – CollinD Sep 04 '15 at 03:34
0

First of all, I think you can refer the way to use Interface and Abstract here:

https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html 
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Back to your current design, I think that way is not really OOP. My suggestion is that change Person to an Abstract class. You can create another class like, Student, Professor... to extends Person. In Person, there are properties of it like firstName, lastName, mNumber and have getter/setter for them. The Faculty is another class which contain the information of the itself only. You will need a Service class, for example StudentManagementService(the name reflects your business or your action). Just try to think like that way and if you still have questions, everyone will help you.

Hope this help.

Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23
0

Interface have abstract methods, which you implement in your Implementation class and since, you have not implemented or overridden any of the methods in Faculty class, the compiler is complaining.

What you can do is make the Interface as a class and then your code would work fine. Or, if you still want to use Interface, then you need to put abstract methods in there and implement them in implementing class. This is Java 7.

If you are on Java 8, then you should do it this way:

public interface MyInterface {
    public void existingMethod();
        default public void newDefaultMethod() {
        System.out.println("New default method is added in interface");
    }
} 

public class MyInterfaceImpl implements MyInterface {
    public void existingMethod() {
        // default implementation is called
    }
}

Even, in this case you will have to provide the method body in your class, but you have the liberty to leave it blank, and when you do that the default method would be called

Sategroup
  • 955
  • 13
  • 29