participate


Java Programming [Archive] - Inheritance
<<   Back to Forum  |   Give us Feedback
5 Duke Stars rewarded for this thread
This topic has 7 replies on 1 page.
madhuribele
Posts:8
Registered: 5/14/03
Inheritance   
Jun 3, 2004 2:23 AM

 
Can main method be inherited ? If a super class has a main method and sub class doesn't have it, will the main method of the superclass is called ?
 
serlank
Posts:951
Registered: 6/7/01
Re: Inheritance   
Jun 3, 2004 2:29 AM (reply 1 of 7)  (In reply to original post )

 
Yes.
 
madhuribele
Posts:8
Registered: 5/14/03
Re: Inheritance   
Jun 3, 2004 2:31 AM (reply 2 of 7)  (In reply to #1 )

 
can you just explain it more clearly ? As far as I know the static methods cannot be inherited and main is a static method. how can it be inherited ?
 
serlank
Posts:951
Registered: 6/7/01
Re: Inheritance      
Jun 3, 2004 2:40 AM (reply 3 of 7)  (In reply to #2 )

 
Static methods are inhereted. Run the following code.
public class Test {
	public static void main(String[] arg) {
		Test1.test();
		Test2.test();
		Test3.test();
	}
}
class Test1 {
	public static void test() {
		System.out.println("Test1");
	}	
}
class Test2 extends Test1 {
}
class Test3 extends Test1 {
	public static void test() {
		System.out.println("Test3");
	}	
}
 
BIJ001
Posts:6,897
Registered: 2003.06.06.
Re: Inheritance   
Jun 3, 2004 3:03 AM (reply 4 of 7)  (In reply to #3 )

 
Caveat:
                Test1 t0=new Test3();
                t0.test();

will yield "Test1", because t0's declared type is Test1, not Test3.
 
mattbunch
Posts:1,860
Registered: 12/14/00
Re: Inheritance   
Jun 3, 2004 3:51 AM (reply 5 of 7)  (In reply to #2 )

 
Can main method be inherited ?
If a super class has a main method and sub
class doesn't have it, will the main method of
the superclass is called ?

The best way to answer this question is to try it and see. It never fails to amaze me how often question like this get posted - it would probably be quicker and easier to just test than to come here, ask the question and wait for an answer. If you can't even manage that much on your own, then you aren't going to get very far as a Java developer.

By all means, once you've tried it for yourself, feel free to come here and ask about the results if they surprised you or you don't understand them, but asking "What would happen if I did this", when its something you could just do and find out, makes you seem a little pathetic (IMHO).


can you just explain it more clearly ?

Explain what more clearly? You asked a yes/no question and got an answer (yes).


As far as I
know the static methods cannot be inherited

Well whoever told you that got it wrong.


A better question to have asked today might have been:
"I read last week that static methods aren't inherited, but I wrote some code that seems to demonstrate that in fact they are. At first I thought it was just the main method but then I tried with other static methods and found the same thing happened. Is it a special circumstance with certain static methods that I've stumbled across, or is what I was told last week just plain wrong? Here's some code to demonstrate what I did: <your code here>"
 
madhuribele
Posts:8
Registered: 5/14/03
Re: Inheritance   
Jun 3, 2004 3:55 AM (reply 6 of 7)  (In reply to #5 )

 
Thank you all for the answer.
 
YoGee
Posts:4,760
Registered: 6/19/02
Re: Inheritance   
Jun 3, 2004 4:26 AM (reply 7 of 7)  (In reply to #2 )

 
As far as I know the static methods cannot be inherited ...

I've made the mistake of saying that before. The correct answer is that static methods are inherited, but they are not polymorphic.
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums