• Post Reply Bookmark Topic Watch Topic
  • New Topic

protected constructor  RSS feed

 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q. Can Protected member of a class invoked in nonsubclss & in different java file?

if NO then try following programs

A.java
==========================================

// class containing protected constructor...


class A
{
public int i ;
protected A()
{
i=123;
}

protected A(int j)
{
i=j;
}

public static void main(String [ ]args)
{
System.out.println("Class A!");

}
}


======================================
B.java
================================
// Class where Class A ( having protected Constructor ) get instantiated.....

// Class where Class A ( having protected Constructor ) get instantiated.....

class B
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A(6);

System.out.println("Derived Class B \n a1.i="+a1.i+"\n a2.i="+a2.i+".......");

}
}



---------------
Conclusion : Protected Constructor gets invoked in nonsubclss.

Why it happens?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amod, comrade, read the Java Language Specification section 6.6.2 named Details on Protected Access

Regards,
Edwin Dalorzo.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected members (not only constructors) are also accessible in all classes in the same package.
So the answer is YES, protected member of a class can be invoked in another class which is not a subclass of the defining class and is in different file.

If A and B were in different packages this wouldn't compile.
 
Thibault Dangr�aux
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a common pitfall for C++ programmers...
In Java, specifying "protected" rather than nothing (package) actually relaxes the security policy, since it makes the qualified item (field or method) accessible from both children and classes from the same package.
private < package < protected < public.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every so often there's talk about "protected-private": http://weblogs.java.net/blog/monika_krug/archive/2005/01/a_protectedpriv_1.html
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
Every so often there's talk about "protected-private":


That's funny -- things have come full circle, then! In the earliest Java releases, pre-1.0, you could declare a member "private protected", using those two keywords together. It was taken out by the 1.0 release. My recollection was that it was removed because the implementation was flawed; it was possible to cast it away.

Another thing I remember from those days: single-line try and catch blocks didn't need braces. There was some hue and cry when this change was made!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!