0

I have a doubt like i have read we can't override private methods here i can override my private method showAge method it is not showing any error . update me if i am wrong.

class parent {
  int age = 26;

 private void showAge() {
    System.out.println("age:" + age);
}

}

public class Inheritance extends parent{

 String name = "karthik";

 void showName() {
    
    System.out.println("Name :" + name);
    
}
 
  void showAge() {
     System.out.println("child age:" + age);
     
 }
 

public static void main(String args[]) {
    Inheritance i = new Inheritance();
    
    i.showAge();
    i.showName();
    i.age = 16;
    i.name = "surya";
    i.showAge();
    i.showName();
    
}

}

1 Answers1

0

Like chrylis said, you aren't overriding it. If you chance the method to 'protected' then you can use it in the 'Inheritance' class and override it if you wish.

Will
  • 151
  • 7