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.

10
  • Does Dog implement or extend Puppy? We don't know 'cause we don't see the code of these types. Commented Aug 14, 2019 at 7:32
  • 1
    Read about Polymorphism in Java. That will help you understand what is going on in there. Commented Aug 14, 2019 at 7: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. Commented Aug 14, 2019 at 7: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). Commented Aug 14, 2019 at 7:38
  • 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. Commented Aug 14, 2019 at 8:00

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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. :)

Comments

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.

Comments

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.

Comments

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

Comments

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).

Comments

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.

1 Comment

dynamic binding? not really, there is no method involved in this question at all (if it is not changed again)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.