• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract and private

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That was really a significant discussion.b
------------------
Regards,
Shree
 
reply
    Bookmark Topic Watch Topic
  • New Topic