• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

in Overriding

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
^
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the superclass method is public, the overriding method must be public. This is one of rules for overriding.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding method can't be more restrictive.
 
Hold that thought. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic