Covariant Return Types

This morning I read the article Turing Award Won by 3 Pioneers in Artificial Intelligence by Cade Metz of the New York Times. The recipients are Yann LeCun, Geoffrey Hinton and Yoshua Bengio.

In the past couple years I have been reading, taking online courses and experimenting with machine learning (ML) which is a discipline of Artificial Intelligence (AI). I have read a few articles from Yann LeCun but I have not watched a video of him yet.

I took a course from Andrew Ng on Coursera. Andrew invites scientist to get different opinions on various topics. Andrew mentioned that one of the best things one can do in the ML field is read (and understand) as many papers on ML you can get your hands on. When Andrew asked Geoffrey he came back saying that what you need to develop and trust your own instincts. Through life professor Hinton has in several occasions leaned towards what he believes is right and with time most of his hunches have proven home runs.

On a separate interview hosted by Andrew Ng he invited Ian Goodfellow. During the interview, Ian brought up a book he had just published “Deep Learning” by Ian Goodfellow, Yoshua Bengio and Aaron Courville. The book is quite interesting and heavy on math. I was going to read the entire book but will skip the third part that is mostly associated with research.

It seems like the ACM award was given to the right group of scientists.

I received a message from Hacker Rank to solve the Covariant Return Types challenge

It is good idea to read a couple references that HackerRank makes to Covariant Return Types and Method Overriding.

The modified output, from my solution, written in the Eclipse IDE follows:

AndhraPradesh
class Lily
Lily


WestBengal
class Jasmine
Jasmine

The modified test code is as follows:

/*
 * 
 */
public class Solution {
	
	/*
	 * 
	 */
	public static void main(String[] args) throws IOException {
		
	      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	      String s = reader.readLine().trim();
	      Region region = null;
	      switch (s) {
	        case "WestBengal":
	          region = new WestBengal();
	          break;
	        case "AndhraPradesh":
	          region = new AndhraPradesh();
	          break;
	      }
	      
	      // ???? ????
	      System.out.println(region.yourNationalFlower().getClass());
	      
	      Flower flower = region.yourNationalFlower();
	      System.out.println(flower.whatsYourName());
	}
}

As you can see the program passes the name of the region. Based on the name, a region class is instantiated. We then call the yourNationalFlower() method which returns an instance of the Flower class. We then display the string returned by the flower class.

My solution follows:

//Complete the classes below
class Flower {
	String whatsYourName() {
		return "I have many names and types";
	}
}

class Jasmine extends Flower {
	
	@Override
	String whatsYourName() {
		return "Jasmine";
	}
}

class Lily extends Flower {

	@Override
	String whatsYourName() {
		return "Lily";
	}
	
}


class Region {
	Flower yourNationalFlower() {
		return new Flower();
	}
}

class WestBengal extends Region {
	
	// **** assigning to a more general type reference is always safe ****
	@Override
	Jasmine yourNationalFlower() {
		return new Jasmine();
	}
}

class AndhraPradesh extends Region {
	
	// **** assigning to a more general type reference is always safe ****
	@Override
	Lily yourNationalFlower() {
		return new Lily();
	}
}

The whatsYourName() method does not return a string with a specific flower. The Flower class is extended using the Jasmine and Lily classes. The method is overridden to return a string each with the name of the associated flower.

The class Region contains the method yourNationalFlower() which returns an object of type Flower. The Region class is extended with the WestBengal and AndhraPradesh classes. Each class overrides the method returning the proper instance of a flower. I assume the flower names are correct for each region. I did not check those facts on Google.

If you are interested, my entire solution is on my GitHub repository.

If you have comments or questions regarding this post or any other post in this blog, or if you need help with any aspect of the SDLC for a project, please leave me a note bellow. Requests for help will not be made public.

Hope you enjoyed this post. Keep on reading and experimenting. That is the best way to learn.

Regards;

John
Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.