user123123123
user123123123

Reputation: 11

What is wrong with class?

I'm a beginner when it comes to programming Java code. I'm having a real tough time on how this class is wrong such as when I'm trying to extend the class. public final class SeatType extends Enum

This is my whole class:

package assignment;


public final class SeatType extends Enum
{

    public static final SeatType AISLE;
    public static final SeatType WINDOW;
    public static final SeatType MIDDLE;
    private static final SeatType $VALUES[];

    public static SeatType[] values()
    {
        return (SeatType[])$VALUES.clone();
    }

    public static SeatType valueOf(String name)
    {
        return (SeatType)Enum.valueOf(assignment/SeatType, name);
    }

    private SeatType(String s, int i)
    {
        super(s, i);
    }

    static 
    {
        AISLE = new SeatType("AISLE", 0);
        WINDOW = new SeatType("WINDOW", 1);
        MIDDLE = new SeatType("MIDDLE", 2);
        $VALUES = (new SeatType[] {
            AISLE, WINDOW, MIDDLE
        });
    }
}

EDIT:

These are the errors.

SeatType cannot be resolved to a variable. assignment cannot be resolved to a variable. The type SeatType may not subclass Enum explicitly. The constructor Object(String, int) is undefined –

Any help is much appreciated :)

Upvotes: 0

Views: 278

Answers (5)

Ashok Bala
Ashok Bala

Reputation: 168

In Java , it wont allow you to extend Enum explicitly.

You can create an enum like below :

    public enum Season {
      WINTER, SPRING, SUMMER, FALL; 
    }

According to the Java Language Specification, Each enum is implicitly final and is a subclass of Enum. So an enum already inherits from another class, making it a subclass of another class would break Java's single inheritance paradigm. An enum can, however, implement an interface

Upvotes: 3

Yogesh
Yogesh

Reputation: 759

Java compiler doesn't allow you to extend ENUM class. Have a look at this answer.

Also, please read this to learn about using enum.

In this case, you can create an enum like,

public enum SeatType 
{
   WINDOW, AISLE, MIDDLE
}

Upvotes: 2

CodeMatrix
CodeMatrix

Reputation: 2154

Your class is wrong because you're extending the Enum class.

You cannot extend from Enum. You declare an Enum class like that:

public enum MyEnum {
    AISLE,
    WINDOW,
    MIDDLE;
}

That will create your Enum. The actual usage will look like that:

MyEnum.valueOf("AISLE");
MyEnum.AISLE;

Upvotes: 2

Sweeper
Sweeper

Reputation: 270890

You don't need to extend Enum at all. You just need to use the enum keyword to declare an enum:

enum SeatType {
    ...
}

You don't need to have values and valueOf methods. They are inherited from Enum.

From the looks of it, your enum seem to have 2 fields - i and s. You should declare them as fields:

int i;
String s;

Then, write a constructor that takes an int and a string that assigns the parameters to the fields:

SeatType(String s, int i) {
    this.i = i;
    this.s = s;
}

Then, you can declare your enum values:

AISLE("AISLE", 0),
MIDDLE("MIDDLE", 1),
WINDOW("WINDOW", 2)
;

So your whole enum looks like this:

enum SeatType {
    AISLE("AISLE", 0),
    MIDDLE("MIDDLE", 1),
    WINDOW("WINDOW", 2)
    ;
    int i;
    String s;

    SeatType(String s, int i) {
        this.i = i;
        this.s = s;
    }
}

Upvotes: 1

pleft
pleft

Reputation: 7905

Why are you extending Enum? In Java you cannot do this. If you want to create an enumeration class you should create a class with the definition

public enum SeatType {
    // your enum code
}

Upvotes: 2

Related Questions