Skip to content

Instantly share code, notes, and snippets.

@pyq
Last active July 15, 2016 19:03
Show Gist options
  • Save pyq/4d087b0c4913f0aeff0a2b06c795b79b to your computer and use it in GitHub Desktop.
Save pyq/4d087b0c4913f0aeff0a2b06c795b79b to your computer and use it in GitHub Desktop.
Polymorphism related
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.IllegalFormatException;
/**
* Created by yongqingpeng on 7/15/16.
*/
class SuperClass
{
void methodA()
{
System.out.println("super methodA");
}
void methodWithSameException() throws FileNotFoundException
{
System.out.println("super methodWithSameException");
}
void methodHasExceptionInSuper() throws FileNotFoundException
{
System.out.println("super methodWithSameException");
}
void methodWithDiffException() throws EOFException
{
System.out.println("super methodWithSameException");
}
}
public class SubClass extends SuperClass
{
void methodA()
{
System.out.println("sub methodA");
}
void methodB()
{
System.out.println("sub methodB");
}
void methodWithSameException() throws FileNotFoundException
{
System.out.println("sub methodWithSameException");
}
void methodHasExceptionInSuper()
{
System.out.println("sub methodHasExceptionInSuper");
}
// can't throw FileNotFoundException (different type)
//void methodWithDiffException() throws FileNotFoundException
// can't throw superType IOException (EOFException extends IOException)
//void methodWithDiffException() throws IOException
// can throw SubEOFException
void methodWithDiffException() throws SubEOFException
{
System.out.println("sub methodHasExceptionInSuper");
}
public static void main(String[] args)
{
SuperClass superTypeSubInstance = new SubClass();
// I can call methodA, sub methodA
superTypeSubInstance.methodA();
// I can't call methodB because no methodB in SuperClass
// superTypeSubInstance.methodB();
// conclusion:
// which method you can call based on referring type (SuperClass)
// which method you will run based on actually instance type (SubClass)
// for the same exception, compile could make sure we handle "FileNotFoundException"
try
{
superTypeSubInstance.methodWithSameException();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
// for case only super class throws exception, we still need to handle this exception
// because compiler only know superTypeSubInstance is super type.
try
{
superTypeSubInstance.methodHasExceptionInSuper();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
// case for both classes have exceptions
try
{
superTypeSubInstance.methodWithDiffException();
}
catch (EOFException e)
{
// if we catch EOFException
// we could still catch SubEOFException
e.printStackTrace();
}
// conclusion
// subclass overridden method can declare same, subclass exception or no exception
// this guarantee compiler always handle all checked exception no matter what the actually object type is
}
}
class SubEOFException extends EOFException
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment