Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:11+00:00 2026-05-26T04:25:11+00:00

I am trying to get to grips with interfaces and their implementation in Java.

  • 0

I am trying to get to grips with interfaces and their implementation in Java.

I have an interface and 3 classes, 2 of which implement the interface. The implementation of the interface requires 2 methods,

  • toString() [which is using @Overide here for Netbeans satisfaction] and
  • wheelCount().

    public interface IVehicle {
    @Override
    public String toString();
    public int wheelCount();
    }

We then have the two classes Bike and Car.

public class Bike implements IVehicle{
    @Override
    public int wheelCount() {
        return 2;
    }
    @Override
    public String toString(){
        String s = "This is a Bike with " + wheelCount() + " wheels!";
        return s;
    }
}

public class Car implements IVehicle{
    @Override
    public int wheelCount() {
        return 4;
    }
    @Override
    public String toString(){
        String s = "This is a Car with " + wheelCount() + " wheels!";
        return s;
    }
}    

Some other class called myClass contains a method to print stuff out… printStuf()

public void prntStuf(IVehicle myVehicle){
    String s = myVehicle.toString();
    System.out.println(s);
}

Assuming an instance of myClass is created, if somewhere in the code we create instances of Bike() and Car(), and we pass these objects into the method prntStuf, what would be the benefit in using either of these assignments.

IVehicle x = new Car()

    IVehicle modeoftransport1 = new Car();
    IVehicle modeoftransport2 = new Bike();

As opposed to… Car x = new Car()

    Car modeoftransport1 = new Car();
    Bike modeoftransport2 = new Bike();

Is there an actual difference or benefit or does it boil down to coding conventions?

From what I can tell, they both have the same effect, in that they both produce the same outcome. I’ve looked through previous questions on here and searched the web but can’t put my finger on it. Hope this makes sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:25:12+00:00Added an answer on May 26, 2026 at 4:25 am

    In your example, the type of the variable doesn’t matter.

    The choice of type is really a choice about what you’re going to do with the variable later. When you say:

    Car modeoftransport1 = new Car();
    

    You’re saying “I am going to be dealing with a new Car in ways where it matters that it’s a Car”; for example, if Car had some other, Car-specific, methods, maybe you’re going to call those. When you say:

    IVehicle modeoftransport1 = new Car();
    

    You’re saying “I am going to be dealing with a new Car in ways where it doesn’t matter what kind of vehicle it is”.

    So, yes, coding convention, or coding style, to a large extent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get to grips with EF4 CTP5. I have two classes
I'm trying to get to grips with Qt's signal and slots mechanism. I have
I'm trying to get to grips with regular expressions: I have a database connection
I am trying to get to grips with programmatically configuring views. I have a
I have been trying to get to grips with Hibernate's inverse attribute, and it
I am trying to get to grips with MS Dynamics CRM 2011. I have
I'm trying to get to grips with the visitor method in Java. I'm trying
I'm trying to get to grips with NHibernate, Fluent NHibernate and Spring. Following domain-driven
I've been trying to get to grips with Core Data and I'm having some
Trying to get the grips of best URI practice in RESTfulness... Let's say that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.