• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic Java extend class question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// single file below, named A.java keeps generating compilation errors, WHY?

//modified from D. Flanagan, Java in a Nutshell, 5th ed, pg 121
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What compilation errors? Don't make us guess.

Also, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You have statements that are not in a method. Put your System.out.println in a main() method.
 
Richard Pan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Knute, I followed your suggestion that ACTIONS ON OBJECTS are put in methods and when done so properly, my program now compiles and executes without error.
class T { int x =1, y=2;
public void prt() {
System.out.println("T class ending: x= " +x+ ", y=" +y); }
}
class S extends T {
int m = 10;
int n = m + x +y;
public void prs() {
System.out.println("S subclass ending: m = " + m + ", n=" + n); }
}
public class A{
public static void main(String[] arg) {
System.out.println("****Program start");
T t1 = new T();
t1.prt();
S s1 = new S();
System.out.println(s1.m);
System.out.println(s1.n);
s1.prs();
System.out.println("****Program ending");
} }

However, is it true my prt() method MUST be called from the instantiating method, main() here?
Can my prt() method be called FROM WITHIN CLASS T ITSELF, using "this" or other constructs? ie can a class invoke its own method?

Thank you for giving a thorough answer!!
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard,

Be sure to UseCodeTags (← that's a link) when posting your programs. You'll get better responses.

Can my prt() method be called FROM WITHIN CLASS T ITSELF



I'm not sure what you mean. A method can call another method in the same case. And you need a main() method to start processing.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the UseCodeTags link worked above... You can find the actual page here
 
Richard Pan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please allow me to try another time:


above code appears in one single file named A.java.

Thanking you in advance! Richard C Pan
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

You have already been told twice that statements must be inside methods (or constructors or initialiser but you probably haven't seen initialisers yet). You need to move that line before the preceding }
 
reply
    Bookmark Topic Watch Topic
  • New Topic