Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do abstract classes like NumberFormat call the methods from their subclasses?

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to Shaad's question about the private constructors, I had tried a small program that makes us access the object of a class that has all the constructors private
this is it


but even if the class constructors are private we could create the object of the class and could access it from outside the class
that also using the static method of the class like this


but in case of abstract classes those are in the java docs I mean classes like NumberFormat, DateFormat etc
we cannot instantiate them even in they are in the same class i.e. because they are abstract
then how can we use the instantiation method like above to get the object of the class?
like this

how is it done in the inbuilt classes ?
please help me
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out this program
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does that mean that we are getting the instance of the class that extends the abstract class ?
and with the use of polymorphism?
means
DateFormat is referring to the subclass?
I will try to write a program for that definityly
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was checking for that
and found out something
like we instantiate DateFormat class as follows

as the DateFormat class is abstract and the object is of the subclass that extends the DateFormat class
I tried the program to check whether we can do it manually or not
when I run the program given by Ankit sir
that gives the name of the class is SimpleDateFormat

I tried that method with my code and got some similar results for my classes
here is it


this prints out

F:\Java\Javaranch problems>java ConcreteClass
Abstract class constructor
in the ConcreteClass constructor
Abstract class constructor
In the AnotherConcreteClass constructor with String argument
class ConcreteClass
class AnotherConcreteClass


My question here is
is this the way DateFormat and NumberFormat classes are created while creating java?
or is this the way that can resemble to that way?
Expecting help from Ankit sir and the great Henry sir
 
Sandy Ghosh
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:according to Shaad's question about the private constructors, I had tried a small program that makes us access the object of a class that has all the constructors private
this is it


but even if the class constructors are private we could create the object of the class and could access it from outside the class
that also using the static method of the class like this


but in case of abstract classes those are in the java docs I mean classes like NumberFormat, DateFormat etc
we cannot instantiate them even in they are in the same class i.e. because they are abstract
then how can we use the instantiation method like above to get the object of the class?
like this

how is it done in the inbuilt classes ?
please help me


At first when you calling this static method, The private constructor is getting invoked inside the class, so it is legal, and then it is returning the object to the caller..
the static method() is analogous to getInstance() method...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic