Jump to content
Sign in to follow this  
newcoder1010

How to call a method

Recommended Posts

Hello,

 

I have my java as follows:

 public class Test_Final {

	 public void test_non_final () {
	  System.out.println("hello");}

	}
public class Test_Final_Caller extends Test_Final{ // separate file
		 public static void main(String args[]) {
			 Test_Final_Caller S1 = new  Test_Final_Caller();
			  System.out.println(S1.test_final_method()); // error here
			  System.out.println(S1.test_non_final());    // error here



		 }
}

I am not able to print hello from Test_Final_Caller. Can you advise?

Share this post


Link to post
Share on other sites

What does the error message say?

 

Your functions don't return anything, so there's nothing to print out to the system.

 

Use this function instead for testing:

public static void main(String args[]) {
  Test_Final_Caller S1 = new Test_Final_Caller()
  S1.test_non_final();
}

Share this post


Link to post
Share on other sites

I'm not sure what you are trying to do but you should probably read some online tutorials on inheritance, upcasting, downcasting, and polymorphism.

Share this post


Link to post
Share on other sites

Actually w3schools.in is unaffiliated with w3schools.com (at least AFAIK)

 

The question you are asking is related to polymorphism.

 

A subclass can be declared as a superclass, as in the following...

Bicycle bike01 = new Bicycle(20, 10, 1);
Bicycle bike02 = new MountainBike(20, 10, 5, "Dual");
Bicycle bike03 = new RoadBike(40, 20, 8, 23);

Assuming the MountainBike and RoadBike classes both extend (inherit) the Bicycle class.

 

See...

 

https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...