aspose file tools*
The moose likes Beginning Java and the fly likes Two stupid questions about inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two stupid questions about inheritance" Watch "Two stupid questions about inheritance" New topic
Author

Two stupid questions about inheritance

E Lan
Greenhorn

Joined: Mar 15, 2004
Posts: 11
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.


Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Question 1:
This is not possible, if you don't construct the super object how should the VM call an extended method ? Or use extended properties ?
Question 2:
also if you define static methods you have to provide a correct method
layout e.g.
public static void m1(){}
you can use static{} only once as a class initializer.
Hope that helps
Originally posted by E Lan:
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.


Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
This line is strange, just think about it. (or look up parrot in your dictionary, just like me )
class zoo extends parrot{
drop "extends parrot", and you'll get the desired output
fish()
zoo()
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Question 1:
The idea, though you aren't restricted to it, behind inheritance and naming of classes is to go from the general to the specific with 'is-a' relationships.
a Parrot is-a Bird is-a Animal, this is good.
a Zoo is-a Parrot, this is bad.
a Fish is-a ?, doesn't extend anything.
Yes, all the constructors will execute when you instantiate a Zoo, as you said, but this will not make the Zoo as you expect. It will instead give you a single Zoo object.
The semantic relationship between Zoo and the others is 'has-a':

Here, Zoo has-a Parrot and has-a Fish
As to how to invoke only the Zoo constructor in your code...you can't. The Java compiler will insert the default (zero argument) constructor of the super class a class extends, unless you specify some other constructor, which may be the super class' or the another constructor of the child class. If you specify another constructor of the child class, and even if you did that 100 times, eventually, a constructor of the super class must be invoked. The only way to invoke only the Zoo constructor is to not have it extend anything. That said, I've mislead you, as the Zoo class would then implicitly extend Object and invoke its default constructor on instantiation.
Question 2:
instance is not a keyword in Java, so your method declarations are incorrect.
The following code shows static hiding in action

output:
A.m1()
B.m1()
A.m1()
A.m2()
B.m2()
B.m2()
By the output, we see that static methods are inherited, and hidden; and instance methods are not inherited, but overidden. The same being true for variables.
E Lan
Greenhorn

Joined: Mar 15, 2004
Posts: 11
Thanks for your answer,that's really helpful to me.
But I still have questions about Question2.
I correct the static methods in this program,and the previous problems are cleard.This time ,the errors are about instance methods,all reasons are "cannot resolve symbol".Does instatnce also must written correct layout like static? And How to written correct layout of instance?Add public ,or void? Or other problem?
I have'nt seen any programs about applycations of static and instance.Just read some ideas form books and take some tests by myself.If it doesn't trouble you ,please tell me about using methods or variables by static or instance.
Thank you sincerely.
E Lan
Greenhorn

Joined: Mar 15, 2004
Posts: 11
Thanks ,Ray Stojonic.
I understand after your explaination.


[ April 07, 2004: Message edited by: E Lan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two stupid questions about inheritance