-3

What does this statement mean:

B b = new C();

Does it mean that b is object of class B and C at the same time? Can anyone clarify this in detail.

I know

B b = new B();

when I create object from class B, but I don't know what this statement mean

B b = new C();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
M. Na.
  • 11
  • 7
  • So, b is object from superclass B ? – M. Na. Jun 15 '16 at 21:33
  • 1
    C is a subclass of class B. E.g. A Ferrari would be a sub class of Car, as it's a type of Car. In the same token, a C here is a type of B. The variable b is allowing you to create a C, but only to access its methods, etc, via the interface of a B class. Imagine that a car has start(), stop(), accelerate() and decelerate(). A Ferrari would also have these methods, but it may also add a igniteTurbo() method. By doing this, `Car car = new Ferrari();`, it allows us to create a Ferrari object, but only allow access to the first four methods, not the igniteTurbo() method. – ManoDestra Jun 15 '16 at 21:36
  • We could also create a `List list = new ArrayList<>();` which would allow us to process multiple types of Cars in the same list via the principle of polymorphism. Each Car may implement its base methods differently, but we can call them via the same interface signature. – ManoDestra Jun 15 '16 at 21:41
  • Ok, thank you very much for your help. – M. Na. Jun 15 '16 at 21:43
  • 2
    One additional note - B can also be an interface, which ManoDestra shown in example with List => B does not need to be a class really ;-) – Betlista Jun 15 '16 at 21:48
  • This clarification is very helpful, thank you very much again. – M. Na. Jun 15 '16 at 21:48
  • You are right Betlista, thank you very much. – M. Na. Jun 15 '16 at 21:51

2 Answers2

3

In this statement, C is clearly has an "isA" relationship with B - i.e., B is either C's ancestor or an interface that C implements.

In other words, you have one of

class B { ... }
class C extends B [ ... }

or

interface B { ... }
class C implements B { ... }

where there could also be combinations of these and B and C could be more than one inheritance step apart, for instance

class B { ... }
class X extends B { ... }
class C extends X { ... }

You're creating a C instance and assigning it to a variable of type B, meaning you'll only be able to use methods visible via B (without explicit casting, at least).

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1
B b = new C();

It means that the reference of B type refers to the instance of C type. The C class is a subclass of the B. In this case, you can use methods that are defined in the B class by using overridden versions of these methods in the A class (if such methods exist). That mechanism called polymorphism.

Imagine, you have two subclasses of the B class, for example, A and C. You will write a general implementation in methods of the parent class. Then you will override a behavior of some methods in the child class to make them more specific.

B b1 = new A();
B b2 = new C();
// the same type of references

b1.performAction();
b2.performAction();
// the same methods, but the different code will be executed 
// if the methods are overridden in the childs
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142