Re: Core java fundamental help
- From: "Kevin" <kevinwiesner@xxxxxx>
- Date: 31 Aug 2006 03:00:51 -0700
well i am not quite sure, but as far as i know this is quite simple.
if you create a class with "extends" then you override the methods, but
not the variables.
if you call "sup.show()", you call a method and so you use the "new"
method of ExtendsShow.
Later you print "sup.str". This is no mehtod and that's why you get the
String from the orignial class.
i hope that helps you..
cheers,
kevin
pras.vaidya schrieb:
Hello all , I am a newbie in java .Learning core java concepts and i
got confused with this piece of code . The output of code is "Extends
Super" . First time it prints Extends because of overriding but why it
prints " super" later.
Thanks in advance.
Code goes as follow
class SuperShow{
public String str ="Super";
public void show(){
System.out.println(str);
}}
class ExtendsShow extends SuperShow{
public String str = "Extend";
public void show(){
System.out.println(str);}
public static void main(String[]args){
ExtendsShow ext = new ExtendsShow();
SuperShow sup = ext;
sup.show();
//ext.show();
System.out.println(sup.str);
//System.out.println( ext.str);
}}
.
- Follow-Ups:
- Re: Core java fundamental help
- From: Kevin
- Re: Core java fundamental help
- References:
- Core java fundamental help
- From: pras.vaidya
- Core java fundamental help
- Prev by Date: Re: Where can I find SNTP server in Java?
- Next by Date: Re: Core java fundamental help
- Previous by thread: Re: Core java fundamental help
- Next by thread: Re: Core java fundamental help
- Index(es):
Relevant Pages
|