wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes in Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "in Overriding" Watch "in Overriding" New topic
Author

in Overriding

murali palaniappan
Greenhorn

Joined: Jun 15, 2006
Posts: 7
class Test1
{
public void method1()
{
System.out.println("super class method");
}
}


class mura extends Test1
{
public static void main(String args[])
{
Test1 t2= new Test1();
t2.method1();
}
void method1()
{
System.out.println("subclass method");
}
}

Here i am getting compiler error saying



mura.java:17: method1() in mura cannot override method1() in Test1; attempting to assign weaker access privileges; was public void method1()


Plz explain this.

Bye
Muralee
^
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
If the superclass method is public, the overriding method must be public. This is one of rules for overriding.
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
hello murali
listen it is the rule of overriding when u override a method u can not
use those specifiers due to which weeker access done
i mean if u will use private in superclass and then override the method
with public then it will not give an error
because access is stronger u can understant in the way of upcasting and downcasting

thanks
Krish
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
Murali,

You cannot override a public with default. The Overriding sequence can be like this

Private --> Default --> Protected --> Public

It works left to right but not right to left

HTH
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Overriding method can't be more restrictive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: in Overriding