When you extend any base class, all the members of base class will get part of the derived class. Based on their access modifiers these memebers get accessed from the new class.
While defining the derived class if you try to change the behaviour of a particular method, with which the signature is already inherited from base class, we call it as overriding. Similarly, if you define any member variable with the same name in derived class we call it as hiding.
In both the cases we are redefining the inherited members. The only difference between these is when you access them. JVM decides which overridden method to call based on the object referrece that curently pointing to. Incase of member fields it's the Class type of the reference it's pointing. Try to run the below program ...
import java.io.*;
class A
{
String str="This String is defined in A";
public void printMsg()
{
System.out.println("This method is defined in Class A");
}
}
public class B extends A
{
String str="This String is defined in B";
public void printMsg()
{
System.out.println("This method is defined in Class B");
}
public static void main(String[] a)
{
B testB=new B();
A testA=testB;
System.out.println(testB.str);//This prints B String
testB.printMsg(); //This prints B Message
System.out.println(testA.str);//But, this prints A String
testA.printMsg(); //This prints B Message
}
}