Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overriding in Java Inheritance

package P1;

public class Base {

   private void pri( ) { System.out.println("Base.pri()");  }

   void pac( ) {  System.out.println("Base.pac()");  }

   protected void pro( ) { System.out.println("Base.pro()"); }

   public void pub( ) { System.out.println("Base.pub()"); }

   public final void show( ) {
       pri();  
       pac();  
       pro();  
       pub(); 
   }    
} 

and

package P2;

import P1.Base;

public class Concrete1 extends Base {
   public void pri( ) { System.out.println("Concrete1.pri()"); }
   public void pac( ) { System.out.println("Concrete1.pac()"); }
   public void pro( ) { System.out.println("Concrete1.pro()"); }
   public void pub( ) { System.out.println("Concrete1.pub()"); }
}

And I'm executing

Concrete1 c1 = new Concrete1();
c1.show( );

Now, the output is shown to be

Base.pri()
Base.pac()
Concrete1.pro()
Concrete1.pub()

Can someone explain why this is so? From what I understood about inheritance, this should have happened:

1) P2.concrete1 inherits P1.Base
2) c1 object of concrete1 is created
3) c1.show() is called. Since P1.Base.show() is public, it can be called.
4) Now, in P2.concrete1 after inheritance, only it's own methods (pri, pac, pro, pub) and P1.Base's inheritable methods (pro, pub, show) are accessible.

Now WHY does it show Base.pri() and Base.pac() in the output when they're not even accessible?

It's clear that I've not got a very clear fundamental understanding of inheritance. Can someone explain this situation and how inheritance is actually 'structured'. I used to think that inheritable methods and fields of the superclass would just superimpose onto the subclass. But that line of reasoning is obviously wrong.

Thanks!

like image 453
user1265125 Avatar asked Sep 15 '12 04:09

user1265125


People also ask

Is inheritance in Java used for method overriding?

There must be an inheritance connection between classes. All the abstract methods in the parent class should be overridden in the child class. If it declared the methods as static or final, then those methods cannot be overridden.

What is the method overriding in inheritance?

Method overriding is a feature that allows a subclass, or a child class, to specifically implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language. Thus, the process of redefining a parent class's method in a subclass is known as method overriding.

Is inheritance required for method overriding?

Rules for Java Method Overriding The method must have the same parameter as in the parent class. There must be an IS-A relationship (inheritance).

What is difference between method overriding and inheritance in Java?

Inheritance enable us to define a class that takes all the functionality from parent class and allows us to add more. Method overriding occurs simply defining in the child class a method with the same name of a method in the parent class .


1 Answers

The short answer is that you can only override methods that are visible. The first two methods, pri and pac are private and package protected respectively. Because nothing outside the class can see a private method, it can't be overridden. Similarly because Concrete1 is in a different package from Base it can't see Base.pac so cannot override it.

What this means is that while you define a pri and pac method in Concrete1, they're just methods that happen to have the same name as the methods in Base, not overrides. The other two methods pro and pub are protected and public respectively, so are visible to Concrete1. As a result the pro and pub methods in Concrete1 are overrides of the methods of the same name in Base.

Because show is defined in Base, it's compiled calling the 4 methods as defined in Base. When executed the JVM looks to see if any are overridden and if they are executes the overridden methods. As explained above, pri and pac are not overridden so the Base versions are executed where as pro and pub are so the Concrete1 versions are executed.

If you were to move the show method into Concrete1 instead of Base then it would execute the 4 methods as defined in Concrete1 as those would be the methods visible to show.

like image 164
EdC Avatar answered Oct 09 '22 20:10

EdC