newcoder1010 Posted November 3, 2016 Share Posted November 3, 2016 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? Link to comment Share on other sites More sharing options...
Ingolme Posted November 3, 2016 Share Posted November 3, 2016 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(); } Link to comment Share on other sites More sharing options...
newcoder1010 Posted November 3, 2016 Author Share Posted November 3, 2016 Now, it is working. Thanks. Link to comment Share on other sites More sharing options...
davej Posted November 4, 2016 Share Posted November 4, 2016 I'm not sure what you are trying to do but you should probably read some online tutorials on inheritance, upcasting, downcasting, and polymorphism. Link to comment Share on other sites More sharing options...
newcoder1010 Posted November 4, 2016 Author Share Posted November 4, 2016 Trying to go thru tutorial in http://www.w3schools.in/java-tutorial/aggregation/. Is it a typo in your site? public class stud{ public static void main(String args[]){ college a = new college(); college b = new univ(); a.move(); b.move(); } } 4th line, Instead of college b = new univ(); , should it be univ b = new univ(); ? Link to comment Share on other sites More sharing options...
davej Posted November 4, 2016 Share Posted November 4, 2016 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 Link to comment Share on other sites More sharing options...
newcoder1010 Posted November 4, 2016 Author Share Posted November 4, 2016 Thank you so much. I have long way to go. Yes, I am going thru online tutorials. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now