I have some questions about inheritance.In fact,I'm not really familiar with inheritance in java.Mabye my concepts are wrong,please correct my idea.
Question1:
class Animal{
Animal(){SYstem.out.println("Animal()");}
}
class Bird extends Animal{
Bird(){System.out.println("Bread()");}
}
class parrot extends Bird{
parrot(){System.out.println("parrot()");}
}
class fish{
fish(){System.out.println("fish()");
}
class zoo extends parrot{
fish f=new fish();
zoo(){
System.out.println("zoo()");}
public static void main(String[] args){
new zoo();
}
We know if we new a class zoo,then it will execute the constructors of classes Animal,Bird,parrot,and fish because of inheritance.So the output is
Animail()
Bird()
parrot()
zoo()
fish()
But if we only want to invoke zoo's constructor zoo() in this inheritance relation,in other words,we want the output is
fish()
zoo()
How can we write program with this condition?
Question2:
class Supper{
static m1(){
System.out.println("Supper static m1()");
}
static m2(){
System.out.println("Supper static m2()");
}
instance m3(){
System.out.println("Supper instance m3()");
}
instance m4(){
System.out.println("Supper instance m4()");
}
}
class Sub extends Supper{
static m1(){
System.out.println("Sub static m1()");
}
instance m2(){
System.out.println("Sub static m2()");
}
static m3(){
System.out.println("Sub instance m3()");
}
instance m4(){
System.out.println("Sub instance m4()");
}
}
public class si{
public static void main(String[] args){
Supper a=new supper();
Sub b=new Sub();
a.m1();
a.m2();
a.m3();
a.m4();
b.m1();
b.m2();
b.m3();
b.m4();
}
}
I want to test the situation of override or hide between static and instance methods in interitance relation.But the above program has errors
static m1()
static m2() in Supper
static m3()
static m4() in Sub
all the reasons are :invalid declaration:return type required
I'm not really understand what does these mean.Please tell me what the problem is and how to make this program correct.
ps.Sorry,my english is poor,I hope my english and questions did'nt make you upset.
Thank you very much for your patient to read this article.