DerDieDasEhochWas
DerDieDasEhochWas

Reputation: 17

Static and dynamic types

I am preparing for an exam in java. Please have a look at the following two exercises .(I have solutions, but the solutions are without explanations) so If anybody could check my explanations, that would be greatly appreciated.

1)

public interface Sale{}
public abstract class Clothing{}
public class Jacket extends Clothing implements Sale{}
public class LeatherJacket extends Jacket{}

Which of the following are possible:

Sale var1 = new LeatherJacket();

is possible, since LeatherJacket is a subclass of Jacket and Jacket implements sale? (I'm just guessing here).

Sale var2 = new Sale();

impossible. You cannot create an object of an interface type.

Clothing var3 = new Clothing();

impossible. You cannot create an object of an abstract class.

Clothing var4 = new LeatherJacket();

possible, but why?

Jacket var5 = new LeatherJacket();

possible, but why exactly?

LeatherJacket var6  = new Object();

not possible, but why not?

Upvotes: 0

Views: 65

Answers (3)

user3437460
user3437460

Reputation: 17454

When you have a hierarchy like this, you can perceived it via a venn diagram:

enter image description here

The following should give you a very good clue what is allowed and not allowed:

Object o = new Clothing();        //not allowed (Clothing is abstract!)
Object o = new Jacket();          //allowed (jacket is a subset of object)
OBject o = new LaatherJacket();   //allowed (leather jacket is a subset of object)

Clothing c = new Object();        //not allowed (Object is not a subset of clothing, cannot assume object is definitely a Clothing)
Clothing c = new Jacket();        //allowed (jacket is a subset of Clothing)
Clothing c = new LaatherJacket(); //allowed (leather jacket is a subset of Clothing)

Jacket j = new Object();          //not allowed (Object is not a subset of jacket, cannot assume object is definitely a jacket)
Jacket j = new Clothing();        //not allowed (Clothing is abstract!)
Jacket j = new LeatherJacket();   //allowed (leather jacket is a subset of jacket)

LeatherJacket j = new Object;     //not allowed (Object is not a subset of jacket, cannot assume object is definitely a leather jacket)
LeatherJacket j = new Clothing(); //not allowed (Clothing is abstract!)
LeatherJacket j = new Jacket();   //not allowed (Jacket is not a subset of jacket, cannot assume jacket is definitely a leatherjacket)

Upvotes: 0

user3437460
user3437460

Reputation: 17454

Clothing var4 = new LeatherJacket();

possible, but why?

This is allowed because LeatherJacket is a derived class of Clothing. It is not instantiating through the abstract class Clothing. It wrote new LeatherJacket() and not new Clothing().


Jacket var5 = new LeatherJacket();

possible, but why exactly?

This is allowed because all leather jackets are Jacket. But the reverse is untrue. It is like all Lions are animals, then you can write it as:

Animal lion = new Lion();  //where Lion extends Animal

But you can't write it as:

Lion lion = new Animal();  //not all animals are lions

LeatherJacket var6  = new Object();

not possible, but why not?

The reason is same as the previous explanation. Object is at the highest hierarchy in Java, every class is a subclass of class Object. Hence being at the top of the hierarchy, this will not be allowed. However, this will be allowed:

Object var6 = new LeatherJacket();  //allowed (all leather jackets are objects)
LeatherJacket var 6 = new Object(); //not allowed (not all objects are leather jackets)

Upvotes: 0

Milaci
Milaci

Reputation: 515

Clothing var4 = new LeatherJacket();

LeatherJacket extends Jacket that extend Clothing.

Jacket var5 = new LeatherJacket();

LeatherJacket extends Jacket

LeatherJacket var6  = new Object();

Object is the superClass of all. You can't do it but viceversa yes Object o= new LeatherJacket();

Upvotes: 0

Related Questions