• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions on his enum

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"What’s going on here? It looks like we created an abstract class and a bunch of tiny subclasses.
In a way we did. The enum itself has an abstract method. This means that each and
every enum value is required to implement this method. If we forget one, we get a compiler error."

But the thing is, I can remove any method from the enums and get the code will still compile.

Can someone explain to me what the OCP8 book is trying to say?

Thanks.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moe,

Are you sure the code compiles without the printHours in any of the enum constants? If I try to remove one of the methods I get this error

<br /> Season> is not abstract and does not override abstract method printHours() in Season.WINTER
 
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There are no contradictions here.
You define enum (which is an "object blueprint") and tell -- "I have an abstract method".
Then you define inside the enum body all possible instances, and each of them is (a) concrete -- because JVM calls implicitly constructor and constructor may be called on concrete class (b) has an implementation of the abstract method
So, all are happy! 8-)
It would fail to compile if any of your declared instance did not implement "printHours", in that case it would remain abstract, and JVM would not be able to create an instance of particular enum constant.
 
Moe Jackson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look, if I remove one of the methods from SUMMER, it compiles without issue.



 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moe:

The construct you used is one that I may have forgotten, because I don't use it often:

SUMMER, FALL {
public void printHours() { System. out .println("9am-5pm"); }
};


Rather than omitting the contents of the block for Summer, which would be this, and which would not compile:

SUMMER { } , FALL {
public void printHours() { System. out .println("9am-5pm"); }
};


The syntax you used says "use this block for both SUMMER and FALL because I want the same implementation for both of them.

So the original statement was still true, if the common one was abstract, then each one needs to have it implemented.
Not necessarily separately, you are allowed to combine them.  But, all abstract methods must be defined concretely for all cases.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange, for me even if I remove the body of SUMMER, the code doesn't compile, I get a different error:

Season is abstract; cannot be instantiated

use this block for both SUMMER and FALL because I want the same implementation for both of them.


Is this some new syntax rule that I'm not aware of?
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting...I confirm that this, which I have done in the past (well, something like this) DOES compile:

jshell> enum Season {
  ...>     WINTER {
  ...>         public void printHours() { System. out .println("9am-3pm");}
  ...>     }, SPRING {
  ...>         public void printHours() { System. out .println("9am-4pm");}
  ...>     }, SUMMER, FALL {
  ...>         public void printHours() { System. out .println("9am-5pm");}
  ...>     };
  ...>     public void printHours() { System.out.println("Did the Poster lie or get confused??");}
  ...> }
|  created enum Season


While I seem to get the same error as you did when I try what the poster attested that they saw working:

jshell> enum Season {
  ...>     WINTER {
  ...>         public void printHours() { System. out .println("9am-3pm");}
  ...>     }, SPRING {
  ...>         public void printHours() { System. out .println("9am-5pm");}
  ...>     }, SUMMER, FALL {
  ...>         public void printHours() { System. out .println("9am-5pm");}
  ...>     };
  ...>     public abstract void printHours() ;
  ...> }
|  Error:
|  Season is abstract; cannot be instantiated
|      }, SUMMER, FALL {
|         ^


I am in JShell, Java SE 17 OpenJDK version there if it matters, but I suspect it is more likely that the OP erred or got confused?
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I reasoned backwards to show what "must have been happening" taking for granted the OP was correctly stating what they saw:

1. In an enum definition, any abstract methods MUST be implemented for all enum values or the definition will not compile.

2. The OP showed a syntax I normally never use, and stated that it compiled.

I now question whether they actually did see it compile, and if so, what the environment was.

I went ahead to explore a few things I had never done with enums, but failed to check that syntax first before I went on with the experiments.

My bad.

Moe -- are you sure that actually compiled?  What version and release of javac were you using at the time?


 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it compiled originally, and if he's not doing a clean build, it's still got the old .class files from the successful compilation the first time?  Depends how the build is being done.
 
Moe Jackson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im using apache netbeans 12.4 with Java 11 installed

 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it seems like my original understanding was correct after all.

If the common one is defined as abstract, then each and every enum value must override it explicitly for the enum definition to compile.

If the common one has a concrete definition, then only the values which wish to override that definition need provide an explicit definition.

 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic