Re: Core java fundamental help



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);
}}

.



Relevant Pages

  • Core java fundamental help
    ... I am a newbie in java .Learning core java concepts and i ... First time it prints Extends because of overriding but why it ... public void show{ ... class ExtendsShow extends SuperShow{ ...
    (comp.lang.java.programmer)
  • Re: Table object
    ... And I'm a little shaky on that K extends Comparable... ... public Listremove(Object key) ... System.out.println(kills); ...
    (comp.lang.java.programmer)
  • Re: JTablle anomaly
    ... IMHO, If you've extends an AbstractTableModel class, you can override ... public void fireTableChanged{ ...
    (comp.lang.java.gui)
  • Re: Overriding paint()
    ... > I was wondering if/how you can override the graphics methods (just ... in this case) of a canvas. ... > public class SomeThing extends Canvas{ ... class ExtCanvas extends Canvas{ ...
    (comp.lang.java.gui)
  • Re: PHP5 and Classes
    ... You should avoid deep inheritance heirarchies. ... Or perhaps you shouldn't override the methods in this manner, ... class mine extends FPDF ...
    (comp.lang.php)