Studyon Minte9.com
Head First (Java)




Inheritance



* Parent method call

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			
			Dog d = new Dog();
			d.test();
		    }
		}

		class Dog extends Animal {
		    @Override
		    public void test() {
			super.test(); 
				// super keyword used to call parent method
		    }
		}

		class Animal {
		    public void test() {
			System.out.println(this.getClass().getSimpleName());
			    // Output: Dog
		    }
		}

@Override annotation  acts as a compile-time safeguard against a common programming mistake.
It will throw a compilation error if you have the annotation on a method you're not actually overriding the 
superclass method.


* Parent constructor call

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			
			Dog d = new Dog();
		    }
		}

		class Dog extends Animal {
		    public Dog() {
			super();
		    }
		}

		class Animal {
		    public Animal() {
			System.out.println("Animal");
			    // Output: Animal
		    }
		}

	The call to super() must be the first statement in each contstructor

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
		    
			B b1 = new B();
			B b2 = new B(10);
			B b3 = new B("1", "2");
			//B b4 = new B("a");
		    }
		}

		class B extends A {
		    public B() {
			super(); // Output: A
		    }
		    public B(int i) {
			super(i); // Output: 10
		    }
		    public B(String a, String b) {
			super(a, b); // Output: 12
		    }
		    
		    public void B(String s) {
			super(s);
			    // Error: Call to super must be the first statement in constructor
		    }
		}

		class A {
		    public A() {
			System.out.println("A");
		    }
		    public A(int i) {
			System.out.println(i);
		    }
		    public A(String a, String b) {
			System.out.println(a+b);
		    }
		    public A(String s) {
			System.out.println(s);
		    }
		}


* This()

	What if you have overloaded constructors that, with the exception of handling different types, all do the 
	same thing?
	You'd like to put the bulk of the constructor code (including the call to super()) in only om of the 
	overloaded constructors. 

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {

			B b = new B();
		    }
		}

		class B extends A {
		    public B() {
			this(10); // default number
		    }
		    public B(int i) {
			super(12); // Output: 12
		    }
		}

		class A {
		    public A() {
			System.out.println("A");
		    }
		    public A(int i) {
			System.out.println(i);
		    }
		}


* Object

	Every class in Java extends class Object.

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			
			Animal a = new Animal(); // extends Object
			
			System.out.println(a.getClass());
			    // Output: class testingapp.Animal
			
			System.out.println(a.hashCode()); // unique object identifier
			    // Output: 1169863946 
			
			System.out.println(a.toString());
			    // Output: testingapp.Animal@64c3c749
		    }
		}

		class Animal {
		}


http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why