0

Here's the common syntax for creating an object in Java:

Puppy myPuppy = new Puppy( "tommy" );

According to this description of Java classes on TutorialsPoint, the components of this syntax are as follows:

Declaration − A variable declaration with a variable name with an object type.

Instantiation − The 'new' keyword is used to create the object.

Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

And according to this Quora article, constructors must have the same name as the class they belong to.

However, I sometimes see Java objects being created where the constructor part of the syntax differs from the class name, like so:

Dog myDog = new Puppy( "tommy" );

I don't understand how the above syntax can be valid. What is it doing? What type of class is myPuppy?

EDIT: Sorry - to make this clearer I have flipped the Dog / Puppy references in that last line of code, as it makes more logical sense for a Puppy class to extend a Dog class.

Community
  • 1
  • 1
Callum
  • 435
  • 1
  • 4
  • 14
  • Does `Dog` implement or extend `Puppy`? We don't know 'cause we don't see the code of these types. –  Aug 14 '19 at 07:32
  • 1
    Read about Polymorphism in Java. That will help you understand what is going on in there. – Amongalen Aug 14 '19 at 07:33
  • 1
    The line you quoted seems wrong. `Dog myDog = new Puppy("tommy");` would work because `Puppy` is a subclass of `Dog`; the other way does not work because not all dogs are puppies. – Jesper Aug 14 '19 at 07:34
  • @Jesper While this specific example doesn't make much sense, it's perfectly valid code if Dog extends the class Puppy (or implements it if Puppy is an interface). – Mathias-S Aug 14 '19 at 07:38
  • Yes it makes more logical sense for a Puppy class to extend a Dog class - I have edited my example code - and I've probably just invalidated a bunch of the answers in the process. Sorry – Callum Aug 14 '19 at 07:42
  • In my example I meant to say that the Puppy class extends the Dog class. I botched the question and now I can't delete it. – Callum Aug 14 '19 at 07:51
  • You could delete the question, it just gives you a warning, because you have already received answers. It isn't a terribly bad question, so dont worry. – GhostCat Aug 14 '19 at 07:59
  • 1
    And of course, the misconception is that you think that the type used on the left hand side of your declaration is in any way "related" to the constructor call on the right hand side. But it isn't. You are calling a constructor, of a specific class ... and the **result** of that is then addresses using the declared type. – GhostCat Aug 14 '19 at 08:00
  • or, that syntax is not common for **creating** an object, it is for declaring a variable (or field) and assigning it a new instance. Syntax for just creating an instance is `new ClassName(...)` – user85421 Aug 14 '19 at 08:25
  • @Mathias-S Strictly you are correct, but it would of course be really not logical if class `Dog` extends class `Puppy`. As if all dogs are puppies... [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) – Jesper Aug 14 '19 at 09:23

7 Answers7

1

It can be possible if Dog is a subclass of Puppy or if Puppy is an Interface and Dog is an implementation for it. This is a classic example of polymorphism in Java.

Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
1

We're talking about two different things here:

1) Create a file that contains a class definition:

MyClass.java

public class MyClass {
  private int someValue;

  public MyClass(int someValue) {
    this.someValue = someValue;
  }
}

2) Create a class that is extending some other class or implements an interface:

MyInterface.java

public interface MyInterface {
  void doSomeAction();
}

MyClass.java

public class MyClass implements MyInterface {
  private int someValue;

  public MyClass(int someValue) {
    this.someValue = someValue;
  }

  public void doSomeAction() {
    // some logic here
  }  
}

In this case you can use your code like you mentioned:

MyInterface someObjectThatImplementsMyInterface = new MyClass(42);

So, one thing is creating a class definition, and other thing is initializing an object that either extends some class or implements some interface. Two different things to consider. :)

mate00
  • 2,727
  • 5
  • 26
  • 34
1

the constructor part of the syntax differs from the class name

It's never the case. Constructors have a strict declaration and their names are always the name of the class they are defined in.

What you've seen is the initialisation of a completely different class called Dog, which inherits from Puppy.

For you, Puppy is a cute Dog. For me, Puppy is a little Rat. For someone else, Puppy could be a young Seal. But they all are puppies, and they all share some common characteristics. Given that, you can work with a Puppy regardless of its actual type.

It's a stupid idea to have Puppy as a subclass of Dog, though. The same goes for MiddleAgedDog, or OldDog. You could simply ask dog.getAge() and if it returns 20 we can agree that this buddy is old.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

It's due to a concept called 'inheritance' in object oriented programming. In your example, you are not creating a Puppy object but a Dog object. Puppy is just an interface or a class of which Dog is inherited.

Jardo
  • 1,939
  • 2
  • 25
  • 45
0

What type of class is myPuppy?

The type of the variable myPuppy is Puppy but (assuming this code compiles) the object/instance that myPuppy is referencing is of the type (or class) Dog.

What is it doing?

This is called inheritance, i.e. if Dog extends Puppy then each dog is a puppy. Thus any field or variable of type Puppy can refer to (or "hold") an instance of anything that extends or implements Puppy. All that's known is that myPuppy is a puppy but not what kind of puppy (let's say there'd be a Wolf extends Puppy as well - myPuppy could then refer to a dog or a wolf).

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

this is because they use inheritance in their Example

you got

public class Puppy{...}

and

public class Dog extends Puppy {
    public Dog(String string) {...}
}

you can now implement the interface of Dog in Puppy:

public class Dog extends Puppy{...}

and then call

Puppy myPuppy = new Dog( "tommy" );

your Puppy is a Dog and inherits the methods of the parent

Phash
  • 428
  • 2
  • 7
0

This is called Dynamic Binding in Java

Dynamic Binding: In Dynamic binding compiler doesn’t decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both parent and child classes have same method .

It simply means when you can have child class object in parent class reference like:

Dog myDog = new Puppy( "tommy" );

To do this your Puppy class should be child class of Dog

JLS 4.10.2. Subtyping among Class and Interface Types

Given a non-generic type declaration C, the direct supertypes of the type C are all of the following:

The direct superclass of C (§8.1.4).

The direct superinterfaces of C (§8.1.5).

And 4.12.2. Variables of Reference Type

A variable of a class type T can hold a null reference or a reference to an instance of class T or of any class that is a subclass of T.

So let say if you have bark method in you Dog class and you have override that in your puppy class too then:

During compilation, the compiler has no idea as to which bark has to be called since compiler goes only by referencing variable not by type of object and therefore the binding would be delayed to runtime and therefore the corresponding version of bark will be called based on type on object.

Community
  • 1
  • 1
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • dynamic binding? not really, there is no method involved in this question at all (if it is not changed again) – user85421 Aug 14 '19 at 08:30