-
public class
Hi there
I created a class in which I tried to override toString() method of
Object(topmost in hierarchy) class. But java compiler have error
saying that I should declare that overriden method as public. After
declaring the method(i.e. toString() ) as public ; the program was
compiled properly? But why does Java force me to make the overriden
method as public?
And surprisingly this doesn't happen in case of user classes. If I
create class A and then I extend a new class B. Then also I override
some method of A in B. i.e. refer to the following
class A{
void fun()
{
}
}
class B extends A{
void fun() // overriden
{
}
}
This gets compiled properly though I have not made fun() in B class as
public. So why does Java forces to make overriden methods public only
in some situations?
-Dhiraj
-
Re: public class
When you override a public method, you must declare the overridden method as
public. That's a rule of Java, you can't take a method private by
overriding it. Your example is not a counterexample to that rule because
you are not overriding a public method.
PC2
Dhiraj Nilange <dhiraj_nilange@yahoo.com> wrote in message
news:3a30f2f8$1@news.devx.com...
>
> Hi there
> I created a class in which I tried to override toString() method of
> Object(topmost in hierarchy) class. But java compiler have error
> saying that I should declare that overriden method as public. After
> declaring the method(i.e. toString() ) as public ; the program was
> compiled properly? But why does Java force me to make the overriden
> method as public?
> And surprisingly this doesn't happen in case of user classes. If I
> create class A and then I extend a new class B. Then also I override
> some method of A in B. i.e. refer to the following
>
> class A{
> void fun()
> {
>
> }
> }
>
> class B extends A{
> void fun() // overriden
> {
>
> }
> }
>
> This gets compiled properly though I have not made fun() in B class as
> public. So why does Java forces to make overriden methods public only
> in some situations?
> -Dhiraj
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks