*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling this()" Watch "calling this()" New topic
Author

calling this()

Rajat Sarkar
Greenhorn

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

public Animal(int x)
{
teeth_number=x;
}

public Animal()
{
this(10);
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Greenhorn

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



Cheers
 
wood burning stoves
 
subject: calling this()
 
Similar Threads
Help Identify the Design Pattern
backgroundColor
Help understanding Abstract Factory from a factory Example
Creating Objects
Run Time Type Identification