• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why the constructors of Calendar class are protected?  RSS feed

 
Dana Ucaed
Ranch Hand
Posts: 300
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Calendar class is abstract.

My question is: Why there are constructors for this class and why there are protected?

From documentation:


protected
Calendar()
Constructs a Calendar with the default time zone and locale.
protected
Calendar(TimeZone zone, Locale aLocale)
Constructs a calendar with the specified time zone and locale.

Thanks in advance!
 
Roel De Nijs
Sheriff
Posts: 11200
174
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:My question is: Why there are constructors for this class and why there are protected?

Let's begin with the first question. Why do you think an abstract class can't have a constructor?
 
Dana Ucaed
Ranch Hand
Posts: 300
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have:

DateFormat df = new DateFormat();
I got an error compiler : DateFormat is abstract and cannot be instanced.

the same issue for Calendar.

So, what is the scope of constructors for abstract class?




 
Roel De Nijs
Sheriff
Posts: 11200
174
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:So, what is the scope of constructors for abstract class?

Is my understanding correct that you think you don't need constructors in an abstract class since you can't instantiate them (as you shown with the DateFormat class)?
 
Henry Wong
author
Sheriff
Posts: 23192
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:
DateFormat df = new DateFormat();
I got an error compiler : DateFormat is abstract and cannot be instanced.


Well... obviously, you can't instantiate abstract classes...

Dana Ucaed wrote:
So, what is the scope of constructors for abstract class?


If you subclass the abstract class, with a concrete class, and then instantiate that concrete class, how do you think the superclass portion of the concrete class would be initialized? With the constructor of the superclass (ie. abstract class), of course.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36864
481
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are protected so you use the static factory methods to get an instance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!