Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

OOPs Concept (Over-riding)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of code below is "Sachin" and not "Nishant", just need explanation for the same...

class BaseClass{

protected String name="Sachin";

public void printName(){
System.out.println(name);
}
}

public class MyClass extends BaseClass{
private String name="Nishant";

public static void main(String[] args){

MyClass myClassObj = new MyClass();
myClassObj.printName();

}
}
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: Try overriding the method.

Also please use code tags when you display code in a POST
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that member variables cannot be overridden. Only methods can be overridden.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps...

class BaseClass
{

protected String name="Sachin";

public void printName()
{
System.out.println(name);
}
}

public class MyClass extends BaseClass
{
private String name="Nishant";

public void printName()
{
System.out.println(name);
}

public static void main(String[] args){

BaseClass myClassObj = new MyClass();
myClassObj.printName();

}

}

Byee....
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic