wood burning stoves
The moose likes Beginning Java and the fly likes calling this() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling this()" Watch "calling this()" New topic

calling this()

Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
public abstract class Animal
private int teeth_number;

public Animal(int x)

public Animal()
System.out.println("These do not execute"); // this line does not execute }

public int showTeeth()
return teeth_number;

public abstract void roam();

Why the underlined line does not execute while creating subclasses of Animal?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

It would execute if the subclass invokes the no-argument constructor of the superclass; but if the subclass uses "super(32)" or otherwise calls the constructor with an int argument, the no-argument constructor will not be executed.

[Jess in Action][AskingGoodQuestions]
Melissa Raven

Joined: Dec 30, 2008
Posts: 4
This ran fine for me:

I agree. Here's the link: http://aspose.com/file-tools
subject: calling this()
It's not a secret anymore!