• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar class add method is abstract, but it works  RSS feed

 
Mike London
Ranch Hand
Posts: 1505
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Calendar class has an add method defined like this:

---------------------------

add

public abstract void add(int field,
int amount)

Date Arithmetic function. Adds the specified (signed) amount of time to the given time field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

add(Calendar.DATE, -5)

-----------------------------------

What's confusing me is that this method in the Calendar class is delcared abstract, which means it doesn't have a method body. But, calling the add method on a Calendar class object returns a date value that's been modified implying it *does* have a method body (somewhere).

What's going on? IOW, how can an abstract method do anything? I can't create an abstract class with a method body.

Any insight would be appreciated.

Thanks.

-- Mike
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you are instantiating your calendar object as you should be using Calendar.getInstance then Calendar is really an instance of some subclass (typically GregorianCalendar) which has implemented the "add" method. This is the beauty of inheritance.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And polymorphism since you can call methods, such as add(), on your Calendar without actually knowing that it's really a GregorianCalendar (i.e. a sub-class or implementation). Cool, huh?

Jules
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!