954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with interfaces

I am new to programming and I don't know how to make interfaces. My code is :

public class CarsDescription {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    interface Domestic {}
    interface Import {}
    interface Japanese extends Import {}
    interface German extends Import {}
    interface Detroit extends Domestic {}
    interface SpringHill extends Domestic {}
    interface Vehicle {}
    interface Automobile extends Vehicle {}
    interface LargeAutomobile extends Vehicle {}
    interface Sedan extends Automobile {}
    interface Van extends LargeAutomobile {}
    interface Truck extends LargeAutomobile {}
    interface Compact extends Automobile {}
    interface SportsUtilityVehicle extends Truck, Van {}
   
    class SaturnSL1 implements SpringHill, Sedan {}
    class HondaCivic implements Japanese, Compact {}
    class MercedesC230 implements German, Sedan {}
    class ChevyS10 implements Detroit, Truck {}
class SubaruOutback implements Japanese, SportsUtilityVehicle {}
    }
}
vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

Interfaces and classes must be outside of method bodies.

public class CarsDescription {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    }

    interface Domestic {}
    interface Import {}
    interface Japanese extends Import {}
    interface German extends Import {}
    interface Detroit extends Domestic {}
    interface SpringHill extends Domestic {}
    interface Vehicle {}
    interface Automobile extends Vehicle {}
    interface LargeAutomobile extends Vehicle {}
    interface Sedan extends Automobile {}
    interface Van extends LargeAutomobile {}
    interface Truck extends LargeAutomobile {}
    interface Compact extends Automobile {}
    interface SportsUtilityVehicle extends Truck, Van {}
   
    class SaturnSL1 implements SpringHill, Sedan {}
    class HondaCivic implements Japanese, Compact {}
    class MercedesC230 implements German, Sedan {}
    class ChevyS10 implements Detroit, Truck {}
    class SubaruOutback implements Japanese, SportsUtilityVehicle {}
}


There is something to be said for putting each and every class and interface into its own file. But that is not required.

JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28
 

I'm not sure I see what it is you're trying to do with these interfaces. What does it buy you to implement (sorry) these categories as interfaces, and not, say, as booleans or as enums or named constants?

I can't think of any method signatures that would be required of a Japanese auto and not a German one....

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

That is quite true: It's one crazy mass of empty classes and marker interfaces. I have no idea what vishal1949's next move will be. But it's sure to be interesting. ;)

JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28
 

I assume he's just trying to get the hang of extending and implementing interfaces and classes, in a course he's following.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This is just the beginning of my assignment. Next I have to add a declaration to the Vehicle interface( int getWeightinPounds) and make a parking garage class that can hold 20 vehicles or 25000 pounds. I am going to have to use charstack(); push(); and pop(); for that.

Could someone tell me whether the following would compile. I got all of them done but am not sure if i am right.

public class VehicleAssignment {
interface Domestic {}
interface Import {}
interface Japanese extends Import {}
interface German extends Import {}
interface Detroit extends Domestic {}
interface SpringHill extends Domestic {}
interface Vehicle {}
interface Automobile extends Vehicle {}
interface LargeAutomobile extends Vehicle {}
interface Sedan extends Automobile {}
interface Van extends LargeAutomobile {}
interface Truck extends LargeAutomobile {}
interface Compact extends Automobile {}
interface SportsUtilityVehicle extends Truck, Van {}
    
class SaturnSL1 implements SpringHill, Sedan {}
class HondaCivic implements Japanese, Compact {}
class MercedesC230 implements German, Sedan {}
class ChevyS10 implements Detroit, Truck {}
class SubaruOutback implements Japanese, SportsUtilityVehicle {}

SaturnSL1 sl = new SaturnSL1();     // This is the part going all the way down
HondaCivic hc = new HondaCivic();
Japanese jp = new Japanese();
German gr = new MercedesC230();
ChevyS10 cs = new Truck();
SubaruOutback sb = new SubaruOutback();
Domestic dm = sl;
dm = hc;
dm = (Domestic) hc;
dm = cs;
dm = (Domestic) cs;
Import im = sb;
gr = im;
gr = (German) im;
jp = im;
jp = (German) im;
jp = (Japanese) im;
Automobile a = cs;
a = hc; // this is the end
Attachments program_for_5.png 91.85KB
vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

I am marking this solved since you have now created another new thread to continue this.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: