File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract and private 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 "abstract and private" Watch "abstract and private" New topic
Author

abstract and private

Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi,
Before you consider the code that follows, there are two main points to note:
1. Inner classes of a class can access the private members of the outer class.
2. When you have a subclass extending the superclass, and you try to override the method in the parent class, the access modifier cannot be made more restrictive, but it can be made more public.
The following is a code that proves that the Java compiler allows an inner class to extend the outer class.
Now, consider the following code:
<CODE>
public class check
{
public class myabstract
{
int i;
String s;
private void mymethod()
{
System.out.println("In myabstract::mymethod()");
}
public void anothermethod()
{
System.out.println("In myabstract::anothermethod()");
}
class myinner extends myabstract
{
private void mymethod()
{
System.out.println("In myinner::mymethod()");
}
}
}
}
</CODE>
The above code also shows one more important thing:
A PRIVATE METHOD CAN BE OVERRIDDEN IN A SUBCLASS!!(Here the subclass is the inner class and it is able to access all the methods(including private) of the parent class.)
Also, any method that is allowable to be overridden in the subclass can be allowed to be marked as abstract in the parent class. Then, why doesn't the compiler allow a private method to be declared abstract???
Thanks in advance,
Aparna
Rajni Singh
Greenhorn

Joined: Dec 05, 2000
Posts: 8
public class check
{
public class myabstract
{
int i;
String s;
private void mymethod()
{
System.out.println("In myabstract::mymethod()");
}
public void anothermethod()
{
System.out.println("In myabstract::anothermethod()");
}
class myinner extends myabstract
{
private void mymethod()
{
System.out.println("In myinner::mymethod()");
}
}
}
}

Hi Aparna !
A private method is not inherited by a subclass under any circumstance. Well, what you are doing is not overriding the method mymethod() in class myinner but creating an entirely new method in myinner as the super class method mymethod() is not inherited by myinner class at all.
Also aprivate method cannot be declared abstract as a private method is not inherited by a subclass whereas an abstract method
requires to be overridden in the subclass. Hence using both private and abstract modifiers is contradictory.
Hope I am clear.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Rajni,
Actually, later I realised that, the inner class is able to access all the private members of the outer class, but it doesn't inherit the private members.
Please have a look at the following code:
<CODE>
public class myabstract
{
int i;
String s;
private void mymethod()
{
System.out.println("In myabstract::mymethod()");
}
public void anothermethod()
{
System.out.println("In myabstract::anothermethod()");
}
class myinner extends myabstract
{
/*
private void mymethod()
{
System.out.println("In myinner::mymethod()");
}
*/
public void myownmethod()
{
super.mymethod();//will give an error if replaced by this.mymethod()
}
}
public static void main(String args[])
{
myabstract obj=new myabstract();
myabstract.myinner innerobj=new myabstract().new myinner();
innerobj.myownmethod();
}
}
</CODE>
In the above code, if
super.mymethod() is replaced with this.mymethod(),
then it would give an error which means to say, though it is able to access that private member, it doesn't actually inherit that private member of the class. The same case works if I put myabstract::mymethod as public. So, the underlying fact here is that, an inner class if it extends the outer class, though, can access the private members of the outer class, DOES NOT inherit the private members of the outer class.
Thanks,
Aparna
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi,
That was really a significant discussion.b
------------------
Regards,
Shree


Regards,<BR>Shree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract and private
 
Similar Threads
How can acces inner class content from outer class if inner class is abstract?
Regular Inner class
Code for Method-Local Inner Classes not compiling
why private member variable of inner class can't be accessed by its outer class but mehod can be?
why private member variable of inner class can't be accessed by its outer class but mehod can be?