Sign in to follow this  
Followers 0
newcoder1010

How to call a method

7 posts in this topic

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0