Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

in Overriding

 
murali palaniappan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the superclass method is public, the overriding method must be public. This is one of rules for overriding.
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding method can't be more restrictive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic