• 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

protected constructor

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amod, comrade, read the Java Language Specification section 6.6.2 named Details on Protected Access

Regards,
Edwin Dalorzo.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic