Skip to content

Instantly share code, notes, and snippets.

View rajeevprasanna's full-sized avatar

Rajeev Kumar kallempudi rajeevprasanna

View GitHub Profile
@rajeevprasanna
rajeevprasanna / Animal.java
Created January 19, 2014 03:25
Has-A relationship example
package inheritanceIsaAndHasA.hasA;
public class Animal {
}
@rajeevprasanna
rajeevprasanna / Animatable.java
Created January 19, 2014 04:41
polymorphism example
package polymorphism;
public interface Animatable {
void animate();
}
@rajeevprasanna
rajeevprasanna / PolymorphismExample.java
Created January 19, 2014 04:56
Compile time polymorphism example
package polymorphism.compileTime;
public class PolymorphismExample {
public void doSomething(Worker worker) {
System.out.println("I'm a worker");
}
public void doSomething(Teacher teacher) {
System.out.println("I'm a Teacher");
@rajeevprasanna
rajeevprasanna / PolymorphismExample.java
Last active January 3, 2016 18:09
Runtime polymorphism example
package polymorphism.runTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//https://gist.github.com/rajeevprasanna/8500647
public class PolymorphismExample {
public static void main(String[] args) {
List workers = new ArrayList();
@rajeevprasanna
rajeevprasanna / Animal.java
Last active January 3, 2016 18:09
Super class method invoker
package polymorphism.superclassMethodInvoker;
public class Animal {
public void eat() {
}
public void printYourself() {
// Useful printing code goes here
}
}
@rajeevprasanna
rajeevprasanna / Animal.java
Created January 19, 2014 05:22
polymorphism with exceptions
package polymorphism.superclassMethodInvoker.exception;
public class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
@rajeevprasanna
rajeevprasanna / Animal.java
Created January 19, 2014 05:36
Example with both overloading overriding method
package polymorphism.overloadingAndoverriding;
public class Animal {
public void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
@rajeevprasanna
rajeevprasanna / Bar.java
Created January 19, 2014 05:41
overloading without overriding
package polymorphism.overloadingWithoutOverriding;
public class Bar extends Foo {
// Don’t be fooled by a method that’s overloaded but not overridden by a
// subclass. It’s perfectly legal to do the following:
void doStuff(String s) {
}
// The Bar class has two doStuff() methods: the no-arg version it inherits
@rajeevprasanna
rajeevprasanna / Animal.java
Created January 19, 2014 06:13
typecasting example
package typecasting;
public class Animal {
void makeNoise() {
System.out.println("generic noise");
}
}
@rajeevprasanna
rajeevprasanna / Ball.java
Created January 19, 2014 06:30
interface extending multiple interfaces
package interfaceExtension;
public class Ball implements Bounceable {
public void bounce() {
} // Implement Bounceable's methods
public void setBounceFactor(int bf) {
}