File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Overiding problem using private Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Overiding problem using private" Watch "Overiding problem using private" New topic
Author

Overiding problem using private

mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 62
class Super{
private void Hello(){
System.out.println("Iam in Super()");
}
}
class Sub extends Super{
private void Hello(){
System.out.println("Iam in Sub()");
}
}
public class Checking{
public static void main(String[] args)
{
Super s = new Sub();
Sub s1 = new Sub();

s.Hello();//iam getting error why ?
s1.Hello();this is Correct


}
}

here iam calling Super class ref s.hello()on Sub class Obj. Iam geting compile time error why ?What is the reason..
I Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 41
Both line in your main method should give a compile time error because you cannot call a private method from outside its class.

For other notes, the Sub class is not overriding the Super class Hello method because we cannot override a private method.
[ January 02, 2008: Message edited by: I Wayan Saryada ]

Website: Learn Java Programming by Examples
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 62
class Super{
private void Hello(){
System.out.println("Iam in Super()");
}
}
class Sub extends Super{
void Hello(){
System.out.println("Iam in Sub()");
}
}
public class Checking{
public static void main(String[] args)
{
Super s = new Sub();
Sub s1 = new Sub();

s.Hello();//iam getting error why ?
s1.Hello();this is Correct


}
}

here iam calling Super class ref s.hello()on Sub class Obj. Iam geting compile time error why ?What is the reason..
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11223

Why are you repeating the question? I Wayan Saryada already gave you the answer.

The variable 's' is of type Super. That means that if you call a method on it, it will only look in class Super, even when 's' really refers to a Sub object. Because the method is private in class Super, you can't call it on s.

Also, private methods cannot be overridden. A private method is only visible in the class in which it is declared. It is not visible in subclasses. The subclass doesn't know what private methods exist in its superclass, so it can't override private methods in the superclass. You should see the hello() method in class Sub as a new method that's totally unrelated to the hello() method in class Super.
[ January 03, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3287

Originally posted by Jesper Young:
Why are you repeating the question?


Jesper, i could see that he changed the program in such a way that method Hello() in subclass having default access modifier and NOT private as in his original post.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
 
subject: Overiding problem using private
 
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture