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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "in Overriding" Watch "in Overriding" New topic

in Overriding

murali palaniappan

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();
void method1()
System.out.println("subclass method");

Here i am getting compiler error saying method1() in mura cannot override method1() in Test1; attempting to assign weaker access privileges; was public void method1()

Plz explain this.

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

Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169

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

S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Overriding method can't be more restrictive.
I agree. Here's the link:
subject: in Overriding
It's not a secret anymore!