2

Trying to get clarifications on when you create a new variable, based on an interface or abstract class, and new it when a specific implementation.

Example:

IBlah blah = new BlahImpl();

How would you read that out?

Is it:

A reference variable blah of type IBlah is instantiated with the class BlahImpl?

And so the reference variable will be bound to IBlah, and hence only have the methods/instance and static variables of IBlah but use BlahImpl classes implementaiton of those properties/vars?

Just trying to get my terminology corrected.

3 Answers 3

8

Interfaces and abstract classes can never be instantiated. What you can do as you have in your example is to instantiate a concrete class but assign the resulting object to an interface.

Consider the following class hierarchy:

    IBlah
      ^
      |
AbstractBlah
      ^
      |
   BlahImpl

If IBlah is an interface and AbstractBlah is an abstract class and BlahImpl is a concrete class, the following lines of code are all invalid:

IBlah blah1 = new IBlah();
IBlah blah2 = new AbstractBlah();
AbstractBlah blah3 = new AbstractBlah();

The following lines of code are both valid:

IBlah blah1 = new BlahImpl();
AbstractBlah blah3 = new BlahImpl();

So you can only instantiate concrete a class but the variable you assign that to can be any super-type (a parent class or interface implemented by the class) of the concrete class.

You can refer to and use the concrete class through the interface or abstract class variable. In fact this is actually encouraged as programming to an abstract class (or interface) makes your code more flexible.

It is possible to create a concrete class from an interface or abstract class in in-place. So if we have an interface like this:

interface IBlah {
    void doBlah();
}

That interface could be implemented and instantiated in one fell swoop like so:

IBlah blah = new IBlah() {
    public void doBlah() {
        System.out.println("Doing Blah");
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Is that really true? You can create an instance using interfaces and abstract classes if you provide a body for them. Maybe I'm splitting hairs here?
What you are referring to are anonymous inner classes and they are also concrete, they may be unnamed but they are concrete. If you create an anonymous inner class and do not implement all of the abstract methods of the abstract class or interface you will not be able it instantiate it.
1

"A BlahImpl object is instantiated, and assigned to variable blah as an implementation of interface IBlah." We could also call it a cast, since there is an implicit cast by assigning it like that.

Edit: your first answer sounds reasonable too, the second one does not.

Comments

0

Maybe this is clearer:

Create new instance of BlahImpl and up cast it to IBlah and assign it to blah.

5 Comments

"cast"? Don't see any (IBlah) code. Perhaps the use of "cast" is misleading?
Upcasting is also casting. The (IBlah) syntax is only required when downcasting.
Up cast in Java is always implicit. The fact that rhs and lhs is not the same means that there is a cast.
If it's implicit, why do we have to say it? It seems like it's just noise in the description.
if it's not mentioned, we never know that blah is IBlah, instead we will think that blah is BlahImpl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.