Переопределение, массивы, коварианты и инициализаторы
Я попытался объединить в одной программе некоторые понятия в OOPS, которые на первый взгляд кажутся взаимосвязанными и запутанными. Если вы можете добавить к этому, было бы здорово. Любые указатели или предложения по запоминанию этих концепций также приветствуются.
package oops_concepts;
public class First {
// final static int j; // final Has to have a initial value
static int []j;
/*
* static { j[2]=1; // Throws ExceptionInInitializerError not
* NullPointerException }
*/
protected int i = m2(14); // forward reference
public First() {
i = 15; // runs after instance initializer
}
protected int m2(int j) {
return j;
}
static int staticMethod() {
System.out.println("Calling Superclass static Method");
return 5;
}
public void callTwoMethods(){
System.out.println("Calling Superclass callTwoMethods Method");
staticMethod();
publicMethod1();
}
protected Object protectedMethod1() throws Exception {
System.out.println("Calling Superclass protectedMethod1");
return new String("Base");
}
public Object publicMethod1() {
System.out.println("Calling Superclass publicMethod1");
return new String("Base");
}
public static void main(String[] args) {
First first = new Second();
System.out.println("Value of i :" + first.i);
Second second = new Second();
String s = second.protectedMethod1(); // prints derived
System.out.println("returned : " + s);
second.callTwoMethods();
// String s = first.publicMethod1(); // Does not Compile missing cast
// Returns Object which has to be cast to String.
Third third = new Third();
First[] firstArr = new First[4];
firstArr[0] = first; // No Exception
firstArr[1] = second;
firstArr[2] = third;
First[] firstArr2 = new Second[4];
firstArr2[0] = first; // No Exception
firstArr2[1] = second;
firstArr2[2] = third;
First[] firstArr3 = new Third[4];
firstArr3[0] = third ;
// firstArr3[1] = second; // throws ArrayStoreException
// firstArr3[2] = first; // throws ArrayStoreException
}
}
class Second extends First {
/*
* public int staticMethod(){ // This instance method cannot override the static variant
* static method from First return 5; }
*/
protected String protectedMethod1() {
System.out.println("Calling sub class protectedMethod1");
return new String("Derived");
}
public String publicMethod1() throws IllegalArgumentException {
System.out.println("Calling sub class publicMethod1");
return new String("Derived");
}
}
class Third extends Second {
/*
* protected String protectedMethod1() throws Exception{ // Does not compile
* System.out.println("Calling sub class method"); return new
* String("Derived"); }
*/
protected String protectedMethod1() throws IllegalArgumentException {
return new String("Derived");
}
}
package oops_concepts.sub_pkg;
import oops_concepts.First;
public class Fourth extends First {
@Override
protected Object protectedMethod1() {
return new String("Fourth");
}
public static void main(String[] args) {
First first = new First();
first.publicMethod1(); // only member visible to First
Fourth fourth = new Fourth();
fourth.protectedMethod1(); // works !
}
}