Is it possible to subclass Date?

1 view (last 30 days)
I have a class called myDate that represents dates in a special format, and I'd like to be able to define the year function for this class so that it returns the year associated with the date (similar to the functionality of the built-in function).
However, when I specify my code like this:
classdef myDate < Date
function y = year(obj)
% code to obtain the year
end
end
MATLAB gives me an error that states "'year' might be used imcompatibly or redefined."
I'm guessing this is because Date isn't technically a class (perhaps) and the year function is already used as a built-in function, but is there a way to implement this? The myDate object doesn't use a serial date or datevec as its underlying implementation, either, and I'm only using the base MATLAB.

Accepted Answer

Sean de Wolski
Sean de Wolski on 14 Feb 2013
No you'll have to write your own. (The reason your getting the error above is because year needs to be inside a methods block.)
Of course you could make the myDate simply be a handle class with static methods year(), month() etc.
That way you don't have to create the object to use them.
y = myDate.year()
  2 Comments
Ricardo
Ricardo on 14 Feb 2013
I have a methods block in my actual class, but I left it out of my code sample for conciseness. The error was happening because I already had a property of the class named year.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!