• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

why this code gives compiler error.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Cow is an inner class. The way you have tried to create an instance of class cow is incorrect.


Animal heyAnimal = new Animal();
Animal.Cow c = heyAnimal.new Cow();

This one might be the problem.
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello

You have 2 issues here:
1. The declaration of an instance of the cow class is incorrect. It is an inner class. It should be instanciated something like

TestPoly.Cow c = new TestPoly().new Cow();

If Cow was declared as a static inner class then instanciation would be as follows:

TestPoly.Cow c = new TestPoly.Cow();

Once this is resolved the second issue comes to light.

2. The second issue is related to access modifier of the saySomething() declared in the Animal class. The rule states "A protected variable is visible within a class, and in sub classes, the same package but not elsewhere.", so heyAnimal.saySomething(); will not work.
[ February 14, 2007: Message edited by: Quintin Stephenson ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Quintin,

I am agree with your 1st point....good.

I am sorry to say but your second point it not correct..see this from K & B

a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As by defination of protected modifier
"
A member declared protected is accessible to all classes in the same package in which the class that declares the member exists.The protected member can also be accessed from a subclass of the class that contains
the member even if the subclass is in a different package. You cannot declare a class protected."
this code should work
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this code.
This is the actual code that does not compile.
 
Quintin Stephenson
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am sorry to say but your second point it not correct..see this from K & B

a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.



Guess again!

I am right, test it with the first fix I suggested applied and you will see the following error:

saySomething() has protected access in chapters.p1.Animal



The reason is that in the main method a new instance of Animal is declared. It is not part of the super/sub-class relationship that is established in creation of the Cow class, and therefore the rule applies.
e.g.
public static void main(String [] args)
{
Animal heyAnimal = new Animal();
TestPoly.Cow c = new TestPoly().new Cow();
// remaining code here
}

Secondly, remember the main is not part of the class instaniation, the method is part of its class and not part of objects (as are all static methods), it is there mainly to provide a command line accessor to the class, and therefore has no infuence or involvement in the relations that exists here. In fact all static methods a class don't even need to reference an instansiation of the class it is part of (Though is would make logical sence to have it refering to an instance of the class ie. an object). Heres an added twist in understanding the overriding/overloading story. If Cow did not override the saySomething() method then c.saySomething(); in the main method would also through the same error message at compile time. Only code internally to the non-static methods and constructs would have access.

Lastly a part that I missed out mentioning is related to the class casting
e.g. heyAnimal=c;
As a result of this implicit class casting, you will end up lossing the use of all the methods provided in the sub-class(Cow) and will be running with methods provided in the Super class only. Proof of this can be see if you had created a non-overriding/overloading method in in Cow e.g.
...
protected void saySomethingAgain()
{System.out.println("jo jo jo!");}
...

and then tried using it with heyAnimal. e.g.

public static void main(String [] args)
{
Animal heyAnimal = new Animal();
TestPoly.Cow c = new TestPoly().new Cow();
heyAnimal=c;
//c.saySomething();
//heyAnimal.saySomething();
heyAnimal.saySomethingAgain();
}
The compilation will fail with the following error:
cannot find symbol method saySomethingAgain().
[ February 14, 2007: Message edited by: Quintin Stephenson ]
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want some one to validate these understanding regarding this question:
" If parent class and drived class are placed in different packages and
method to over ride in parent class, is labeled with protected, and overriding method in drived class is labeled with public. Then if we
refer a Parent type Obj.Reference to drived class and access that overridden method, COMPILER ERROR COMES"
Is there any GURU who will help in it
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rashid --

Your display name is still not acceptable -- in fact it's worse now than when Henry warned you about it. We need two names, a first and last name, and both must be real names. Please try again. If you ignore this warning your account will be closed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic