-2

I am beginner in Java. I am confused if the followings are correct or not.

The first condition is understood and is used frequently. What are the usages of others? When and how can I utilize them?

/* 1) */ Car car = new Car();
/* 2) */ Car car = new Vehicle();
/* 3) */ Vehicle car = new Car();
/* 4) */ Vehicle vehicle = new Vehicle();

Perhaps you could include examples in your answer.

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • 2
    possibly related: https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – OH GOD SPIDERS Apr 29 '19 at 14:25
  • 1
    How are `Car` and `Vehicle` related? Does one extend the other? –  Apr 29 '19 at 14:25
  • 2
    If I understand correctly (Car is a subclass of Vehicle), `Car car = new Vehicle();` will not compile because a Vehicle is not a Car, but a Car is a Vehicle. – Benjamin Urquhart Apr 29 '19 at 14:26
  • 6
    [The Java™ Tutorials: Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) – 001 Apr 29 '19 at 14:27
  • Vehicle mVehicle = (Vehicle) new Car(); // see always prefix variables with something "m". I've casted Car to Vehicle. This should work because Cars are always Vehicles. – danny117 Apr 29 '19 at 16:25

2 Answers2

1

Usually you should just declare variables by the most general interface that you are going to use (e.g. Iterable, Collection, or List), and initialize them with the specific implementation (e.g. ArrayList, LinkedList or Arrays.asList()).

Otherwise you're limiting your code to that specific type, and it'll be harder to change when you want to.

For example, if you're passing an ArrayList to a void method(...):

// Iterable if you just need iteration, for (String s : strings):
void method(Iterable<String> strings) { 
    for (String s : strings) { ... } 
}

// Collection if you also need .size(), .isEmpty(), or .stream():
void method(Collection<String> strings) {
    if (!strings.isEmpty()) { strings.stream()... }
}

// List if you also need .get(index):
void method(List<String> strings) {
    strings.get(...)
}

// Don't declare a specific list implementation
// unless you're sure you need it:
void method(ArrayList<String> strings) {
    ??? // You don't want to limit yourself to just ArrayList
}

Another example would be always declaring variable an InputStream even though it is usually a FileInputStream or a BufferedInputStream, because one day soon you or somebody else will want to use some other kind of InputStream.

Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58
-1

In many cases the superclass (or interface) describes a general Object but the subclasses differ in implementation to make the code either more efficient or more accessible.

A good example is the interface List. It has two main subclasses AbstractList and AbstractSequentialList. These provide implementations of most methods in List, but leave the actual memory management up to their subclasses.

When I create a List object, it's usually because I want a length-changing array and not because of ArrayList's exact implementation (Although it does specify a few more useful methods which you can't access in List). This means I just say

List<?> list = new ArrayList<?>();

because I want a list but have to also specify the implementation that's most efficient for my cause.

When it comes to your Vehicle and Car classes, I say it depends on what you want to achieve. In this case specifying your Car object as follows is probably the most sensical:

Car car = new Car();

because you are asking for a car specifically and aren't just using Car as a certain implementation of Vehicle.

In conclusion:

SomeClass c = new SomeClassSuperclass();

is most useful and readable when SomeClass is just an implementation of SomeClassSuperclass.

SomeClass c = new SomeClass();

is most useful and readable when SomeClass is supposed to be a different type of SomeClassSuperclass, but still has it's own features that make it a SomeClass rather than a SomeClassSuperclass (Think of a car in real life, you don't point to a car and say "Look, An awesome vehicle!" or "Look, I got a new vehicle!", as those phrases don't really give you much information about what you are talking about)

J. Lengel
  • 570
  • 3
  • 16