Thursday, August 15, 2013
Return Type Covariance, The Forgotten Form of Polymorphism
First, the basic definition: polymorphism is the idea that there's more than one way to satisfy a contract. Say I have a class called Animal, another called Dog and a third called Cat. Dog and Cat both extend Animal, to use the Java terminology. Without going into the details, anywhere any code expects an Animal, you can supply a Dog, a Cat, or any other object that extends Animal. We say that Dog and Cat are substitutable for Animal because anywhere you need an Animal you can substitute a Dog or a Cat. There's a lot more to be said about that, and maybe I will at some point, but for now I'm moving on to return type covariance.
If Dog and Cat are both derived from Animal, then they could be said colloquially to be more specific than Animal. The set of all Dogs is a subset of Animals. Dog and Cat are partitions of Animal. Dog and Cat are subtypes of Animal, and Animal is a supertype of Dog and Cat. Computer scientists would say that Dog and Cat are both narrower than Animal.
All of those statements express the same concept.
Here's some code for reference:
public class Animal
{
public String speak()
{
return "Basic animal noise";
}
}
public class Dog extends Animal
{
@Override
public String speak()
{
return "Woof";
}
}
public class Cat extends Animal
{
@Override
public String speak()
{
return "Meow";
}
}
Do you see that there's a method named speak() defined in Animal that's redefined in both Cat and Dog? And the @Override annotation that precedes those redefinitions? Yeah, yeah, standard textbook polymorphism. But have you ever done this?
public class Animal
{
...
public Animal getSelf()
{
return this;
}
}
public class Dog
{
...
@Override
public Dog getSelf()
{
return this;
}
}
public class Cat
{
...
@Override
public Cat getSelf()
{
return this;
}
}
Even though Animal defines the return type of getSelf() as Animal, Dog and Cat have successfully redefined that (as evidenced by the @Override annotation) to return instances of their own narrower type. That's legal Java, and it's pretty useful at times. They have redefined a method using a return type that's narrower than that returned by the method declaration that they're overriding. Catch that? Narrower return type? That's what "return type covariance" is.
Why is that ok? It's because the original method definition promised an Animal, and that's exactly what you're getting from all three. Dog is an Animal. Cat is an Animal. So it's ok if they declare the fact that you can expect a narrower form of Animal, right?
So the next time someone asks you what polymorphism is, don't just spout off the usual pablum about redefining methods in subtypes and such. Mention return type covariance. They'll be impressed.
Happy coding!
Sunday, July 15, 2012
Learning Functional Programming with Scala
- It's open source, so there's little financial cost of entry.
- It's built on the Java Virtual Machine (JVM), so whatever I write will be runtime compatible with anything I do or have done in Java.
- There's an Eclipse Plugin for that, so I get to use my favorite development environment.
- It seems to be maturing. Scala is actively maintained, has a good user community, and people are writing helpful things about it. (I hope eventually to be one of the latter.)
So here's what I'm going to do...
- I'm going to get a Scala environment installed on my machine.
- I'm going to start fooling around with tutorials and such.
- I'm going to write something that I can share with the world. It'll run wherever Java runs (that I am able to test), meaning that at minimum I want it to be able to run on my desktop, on the web (including as a Facebook app) and on my Android.
If you've ever thought about following this blog, or following me on Facebook or Twitter, now would be a good time.
Busy family person disclaimer: I don't know how consistently I'll be able to pursue this. This is strictly a time-available kind of thing. Why? Little people are competing for my time, and they win. But they sleep more than I do, so I think I have a fighting chance to get a post or two out once in a while.
Monday, November 29, 2010
AMP-lify Your Career with a Master's Degree in Systems Engineering from UVa!
The University of Virginia Accelerated Master's Program in Systems Engineering (AMP) is designed for working professionals eager to enhance their skills and advance their careers. Students from the Mid-Atlantic region travel to Charlottesville for classes all day every other Friday and Saturday with a week in residence at the beginning and end of the program. Two weeks in residence plus 20 Fri./Sat. weekends leads to a master's degree in just one year (May - April) from one of the top universities in the country.
You are cordially invited to an AMP Open House on Saturday, December 4, from 9 am - 1 pm, at the Darden Graduate School of Business in Charlottesville.
Attending an Open House is a great way to learn more about the Accelerated Master's Degree in Systems Engineering. You will meet the academic director and other faculty members, hear highlights of the program and AMP-V, our new options for veterans, participate in a mini-class, talk with current students during breaks, lunch, and at a panel discussion, and tour the Darden School and lodgings. There will also be a short session about education loans that can help make the program accessible to all. The Open House starts with a continental breakfast meet-and-greet and concludes with lunch in the Abbott Center Dining Room (both complimentary). You are welcome to bring your spouse or significant other. Invite friends and colleagues to come explore the program with you.
Register to attend via a short online form at http://www.surveymonkey.com/s/Q3HBYNJ, from the sign up link on the AMP web site, www.sys.virginia.edu/accelerated, or by emailing accmp@virginia.edu. We hope to see you there!
Monday, June 14, 2010
KitchenAid Contest
So here's the deal: just go to http://savingdinner.com/contest/ and follow the directions. Simple!
Happy mixing!
Monday, October 20, 2008
Twitter Returns to the Past
Be sure to read the tooltip. (That means put your mouse anywhere over the comic and leave it there for a moment. An additional message will appear. Read it quickly.)
Tuesday, October 14, 2008
Twitter Revisited
Remember Christmas morning when you were a kid? I don't know about your experience, but for me Christmas morning was great. A bunch of new stuff to play with, all of it bright and shiny and new. I played with everything, all at once. By afternoon I had settled down into playing with one thing at a time, but still wanted to give everything its due.
But after a couple days, maybe a week or a month or two--memory fades with time, and it's been a while--the novelty wore off and my new Christmas presents somehow became just toys. I still played with them. Some of them. But they weren't really special anymore, they were just there.
Twitter is like that, only smaller and not as much fun. EB and I enjoyed twittering at each other for the first hour or two. The rest of that day and the day after we half-heartedly updated our respective statuses because, hey, we made a deal. We were in this together, experimenting. Then I went away for a weekend, didn't want to be bothered by Twitter, and when I got back EB and I both agreed that we'd had enough.
So there's the answer: Twitter isn't all that. It's not even part of that. For us, it completely lacked even the faintest whisper of that. And that, as they say, is that.
Until just now when I read yet another blog post from a friend. Turns out he uses Twitter as part of a convoluted chain of technologies that allow him to post text messages from his cellphone to his Facebook page (not just his status, but right to the wall) and to the homepage of his public website. And, in the process, he also gets his blog posts to his Facebook page and his public website. Cool stuff.
So ok, Twitter has its uses. If you want to turn SMS text messages into an RSS feed so you can pipe it somewhere, great, use Twitter. And if you're an uber-blogger or other cyber-celebrity whose groupies want to know where you are and what you're doing at all times, great, use Twitter. But for me, yawn. I wish the Twitter people well, I just wish them well without me.
Saturday, September 6, 2008
What about Twitter?
What about Twitter? I mean, that's kind of what Twitter is all about, right? And I've always kind of been, well, appalled is too strong, but mildly repulsed by the whole concept of updating my status whenever I turn around. But it's kind of fun, too, in a really narcissistic kind of way. So I was wondering...
Do any of my friends use Twitter? Would anyone care if I'm driving to work, or sitting in a bar listening to Junk Science (great band, btw), or whatever? So tell me...
What about Twitter?
P.S. In case you don't know wtf I'm talking about: http://twitter.com/