• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

find the error...

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody


class Animal
{

Animal(String s)
{
System.out.println(" animal constructor" + s);
}

public static void testClassMethod()
{
System.out.println("The class method in Animal.");
}

public void testInstanceMethod()
{
System.out.println("The instance method in Animal.");
}
}


class Cat extends Animal
{

public static void testClassMethod()
{
System.out.println("The class method in Cat.");
}
public void testInstanceMethod()
{
System.out.println("The instance method in Cat.");
}

public static void main(String[] args)
{
Animal a1 = new Animal("hello");
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}

And i am receving the error ::

D:\Program Files\Java\jdk1.5.0_10\bin\myprograms>javac Cat.java
Cat.java:21: cannot find symbol
symbol : constructor Animal()
location: class Animal
class Cat extends Animal
^
1 error

please help me out...
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose there is a class by name ABC and if you do not write/provide any constructors then compiler will insert a default constructor something like this:



This will be written as following code by compiler:



So in your example, super class has one argument constructor. In this case compiler will not insert default constructor. Your sub class Cat does not have any constructors, so it will try to call super() but there is no zero argument constructor in Animal class.

Hence compilation error.

I hope this clears your doubt.

Murali...
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robin,

You have to provide a no argument constructor for Animal class.

Class Cat extends Animal and Cat will try to make a super() call from its default constructor. Since, compiler doesn't find such a constructor in the super class, it will raise a compiler error. Class Cat will actually looks like the following...

[ July 19, 2007: Message edited by: Al Mamun ]
 
robin singal
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to both of you ...I got the point thanks!!
 
It will give me the powers of the gods. Not bad for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic