Wednesday, December 22, 2010

Java-Testing Inheritance



class TestInheritance {
public static void main(String[] args) {
SomeClass sc = new SomeClass();
Shape s = sc.getShape(Integer.parseInt(args[0]));
s.draw();
if (s instanceof Square) {
Square sq = (Square) s;
System.out.println("It's an instance of Square.");
sq.someFunction();
} else if (s instanceof Circle) {
System.out.println("It's an instance of Circle.");
} else if (s instanceof Triangle) {
System.out.println("It's an instance of Triangle.");
}
}
}

class SomeClass {
Shape getShape(int option) {
Shape s = null;
switch (option) {
case 1:
s = new Square();
break;
case 2:
s = new Circle();
break;
case 3:
s = new Triangle();
}
return s;
}
}

class Shape {
void draw() {
System.out.println("This is an unknown shape.");
}
}
class Square extends Shape {
void draw() {
System.out.println("This is a square.");
}
void someFunction() {
super.draw();
}
}
class Circle extends Shape {
void draw() {
System.out.println("This is a circle.");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("This is a triangle.");
}
}




---Case Study---
java TestInheritance
ArrayIndexOutOfBoundException
-> args[0] is not initialized but is referred

java TestInheritance 0
NullPointerException
-> Shape s = null; -initially
No case in the switch is matched and hence null is returned.
s.draw() {.draw()} causes the NullPointerException

java TestInheritance 1
This is a square.
It’s an instance of Square.
This is an unknown shape.
-> case 1 in the switch is matched and an object of Square is returned from getShape(). It is assigned to s [up-casting: super-class object = sub-class object. sub-class object is wrapped inside a super-class object]. It is down-casted [unwrapped it from the cover of super-class that hinders the visibility of sub-class specific fields and methods] to type Square after proper checking using instanceof before actually using it.

java TestInheritance 2
This is a circle.
It’s an instance of Circle.
-> case 2 in the switch is matched and an object of Circle is returned from getShape(). It is assigned to s [up-casting: super-class object = sub-class object. sub-class object is wrapped inside a super-class object]. It should be down-casted [unwrapped it from the cover of super-class that hinders the visibility of sub-class specific fields and methods] to type Circle after proper checking using instanceof before actually using it.


java TestInheritance 3
This is a triangle.
It’s an instance of Triangle.
-> case 3 in the switch is matched and an object of Triangle is returned from getShape(). It is assigned to s [up-casting: super-class object = sub-class object. sub-class object is wrapped inside a super-class object]. It should be down-casted [unwrapped it from the cover of super-class that hinders the visibility of sub-class specific fields and methods] to type Triangle after proper checking using instanceof before actually using it.

java TestInheritance square
NumberFormatException
-> “square” [String type] cannot be parsed into int using Integer.parseInt(String);


0 comments:

Post a Comment

Contact Form

Name

Email *

Message *