• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Can sumone explain this to me? regarding attributes

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why is the output "easy"?

thx
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first you are missing a semi colon. try to find it.
second if you put this semicolon back the output is not easy the output is :
Cannot find symbol
symbol : method printDifficulty()
location: class SCJPExam
myExam.printDifficulty();

regards,
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think he meant to have SCJPExam extends Exam.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Lynn,

with inheritance the output is easy.
the method in protected so it can be inherited.
what about the member variable ?
does the subclass has 2 variables with the same name and 2 different values ???
or is it just that the method access the member in its class first ??

Can you explain this Lynn?

regards,
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a thead about this.

https://coderanch.com/t/251701/java-programmer-SCJP/certification/Inheritance
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Exam {
protected String difficultyLevel="easy";
public void printDifficulty(){
System.out.println(difficultyLevel);
}
}

class SCJPExam extends Exam {
private String difficultyLevel="killing";
//public void printDifficulty(){
//System.out.println(difficultyLevel);
//}
public static void main(String args[]) {
SCJPExam myExam=new SCJPExam();
myExam.printDifficulty();
}
}

Since the subclass SCJPExam did not override the printDifficulty() method super class's printDifficulty() method is invoked and hence prints easy.

If the comments are removed in the above program it would print:
killing

Thanks
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic