Here I will explain the concepts of overriding through example code
To understand the reason go through the comments, but do not copy paste the code, it will generate error in your IDE, you can download the code from My Git Repository
import java.lang.IllegalAccessException;
// A class with some methods
public class SamsungNote2 {
public void openInNote2() {
System.out.println("you are using note2");
}
protected void playWithNote2(String whatToPlay) {
System.out.println("you are playing with argumented method");
}
public Object note2Feature() {
System.out.println("i will deal with covariant return ");
return null;
}
public void exceptionInNote2() throws IllegalAccessException {
System.out.println("i will explain exception");
}
public void ExceptionalException() throws Exception {
System.out.println("i behave different in polymorphism condition");
}
}
Second class Extending Superclass
Output:
i am in note 3 exception
you are using note2
i m in note 3
covariant return ==> as String is subclass of Object so allow
i am overriden properly in note3
Another class with main method to check polymorphic behaviour
output:
i m in note 3
i am overriden properly in note3
covariant return ==> as String is subclass of Object so allow
i am in note 3 exception
i will be called afther handling the exception,if not handled compilere thinks about superclass only note2!
package in.technoscience.overriding;
//another class called as subclass extending superclass
public class SamsungNote3 extends SamsungNote2 {
// method being overridden
@Override
public void openInNote2() {
// if you wish to call method of superclass first then, back to subclass
super.openInNote2();
System.out.println("i m in note 3");
}
/*
* while overriding changing the argument type if we change the argument
* type then it will not be overridden method it will become new method for
* subclass called as overloaded
*/
// @Override
// protected void playWithNote2(int whatToPlay) {
// }
/*
* // here focus on accessifier in super class we have used protected but
* while overridding public and its alryt, so it satisfy accessifier rule
*/
@Override
public void playWithNote2(String whatToPlay) {
System.out.println("i am overriden properly in note3");
}/*
* // now using higher restriction than superclass compiler is angry now...show red red red red..
*/
// @Override
// private void playWithNote2(String whatToPlay) {
// super.playWithNote2(whatToPlay);
// }
// focus on return type
// in superclass return type is object,here string and compiler is happy
@Override
public String note2Feature() {
System.out.println("covariant return ==> as String is subclass of Object so allow");
return "happy";
}
/*
* // focus on Exception // superclass throws checked exception and here
* this throw runtimeexception== compiler happy
*/
@Override
public void exceptionInNote2() throws ArithmeticException {
System.out.println("i am in note 3 exception");
}
// superclass is throwing exception but subclass is not
// compiler is happy here
@Override
public void ExceptionalException() {
System.out
.println("i will be called afther handling the exception,if not handled compilere thinks about superclass only note2!");
}
/*
* // comliper is angry now, in subclass we are trying to throw new
* checked exception and this is not allowed
*/
// @Override
// public void exceptionInNote2() throws SQLException {
// TODO Auto-generated method stub
// super.exceptionInNote2(); }
// here compiler is again angry
// we are trying to throw exception higher in hierarchy than exception
// thrown in superclass method
// @Override
// public void exceptionInNote2() throws Exception {
// super.exceptionInNote2();
}
package in.technoscience.overriding;
import java.io.FileNotFoundException;
// a class with main method to test the concepts
public class Tsting {
public static void main(String[] args) {
// an object of subclass
SamsungNote3 sn3 = new SamsungNote3();
// check first are those methods inherited
sn3.exceptionInNote2();
sn3.openInNote2();
sn3.note2Feature();
sn3.playWithNote2("note3");
}
}
Output:
i am in note 3 exception
you are using note2
i m in note 3
covariant return ==> as String is subclass of Object so allow
i am overriden properly in note3
Another class with main method to check polymorphic behaviour
package in.technoscience.overriding;
public class TestingOverridingInPolymorphism {
public static void main(String[] args) {
// reference type of super class and object creation of subclass
//runtime polymorphism.
SamsungNote2 sn4= new SamsungNote3();
// due to polymorphism it will call the overriden method.
sn4.openInNote2();
sn4.playWithNote2("polymorphically");
sn4.note2Feature();
try {
sn4.exceptionInNote2();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// in subclass method have not thrown any exception, but
// compiler sees left side of assignement operator and
//still thinks that we want to call method of super class
//so suggest us to handle the exception.
//sn4.ExceptionalException();
try {
sn4.ExceptionalException();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
output:
i m in note 3
i am overriden properly in note3
covariant return ==> as String is subclass of Object so allow
i am in note 3 exception
i will be called afther handling the exception,if not handled compilere thinks about superclass only note2!
It's really a good website. I think there should be a theory also so that it can appear as a good java tutorial.
ReplyDeleteWell too write blogs on java, URL is given below:
http://sanjaymadnani.wordpress.com/
I have also provided the theory regarding overriding link is below:
Deletehttp://catchthetrendz.blogspot.in/2014/04/object-programming-language-part7.html