• 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

regarding inheritance

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
please someone expain me the following code snippet:




According to me it should print "Girrafe". please clarify if i am correct or not?
thanks in advance
[ July 23, 2007: Message edited by: debasmita pattnayak ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's easy to test, isn't it?

What I mean by that is the code contains a problem that will prevent it from compiling, but once you fix that, it will run. (Actually, it contains 3 problems, but 2 of those are just trivial typos.)
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ulf,
to my surprise i am not able to find any errors so that the code doesnt compile..
please explain....

thanks for your support...
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the code you posted is missing a closing "}" before "class Dog", and it references something called "Srting". Those are the trivial problems. Once you fix those, the Dog class still won't compile. Can you tell us why that is?
 
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,the Dog class won't compile..you need write a parameterized super() inside the Dog method.

class Animal{
void method() throws Exception{}
Animal(String name){
System.out.println(name);
}
}
class Dog extends Animal{
Dog(){
super("Gf");
}
void method() throws Exception{}
}
class Test1{
public static void main(String args[]){
new Animal("Girafee");
}
}
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Depasmita!

This is indeed a tricky question. After eliminating the typos already mentioned by Ulf you get the following three classes as a starting point:


The code doesn't compile, because you've implemented a constructor with an non-empty argument list in Animal, but Dog requires a constructor with no arguments. Just to be clear: The compiler adds a Dog()-constructor along with a super()-call when Dog is compiled. This super()-call needs an Animal()-construtor, but it doesn't exist!! Note: the compiler adds a non-argument-constructor at compile-time only if there is no other constructor implemented.


Following two alternatives to solve the problem (class Test always stays unchanged):
1. Implement a constructor in Dog that explicitly calls the Animal(String)-constructor:

Note: don't forget super(name), since otherwise the compiler would add a super() call - and Animal() doesn't exist!


2. Implement an non-argument constructor in Animal

Animal() will be invoked in case you instantiate Dog or if you execute
new Animal();


Cheers,
Robert
 
Don't touch me. And dont' touch this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic