tag:blogger.com,1999:blog-694855878384792308.post5162693814770038065..comments2024-03-21T06:26:49.387-07:00Comments on Java67: Can You Override Private Method in Java ? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-694855878384792308.post-7624535357065655342019-05-21T03:29:56.950-07:002019-05-21T03:29:56.950-07:00Yes sir you are right. It is 2 different things d...Yes sir you are right. It is 2 different things declaring same method with private in super and sub class.. . And overriding is super class method to sub. Supper class method is private and we can't override it. .. .Rajendra Mahakalihttps://www.blogger.com/profile/07556891288387986180noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-22410264576026347372017-10-12T09:20:06.644-07:002017-10-12T09:20:06.644-07:00truetrueAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-18903097848179750402017-02-26T11:17:44.820-08:002017-02-26T11:17:44.820-08:00Another thing that can be noted is that private me...Another thing that can be noted is that private method can't be overriden even if it's visible - for example in an Inner or Nested class. This example code will give a compilation error even though the private method from the base class is visible inside the inner child class:<br /><br />class BaseClass {<br /> private void method() {<br /> System.out.println("In parent");<br /> }<br /><br /> class ChildClass extends BaseClass {<br /> @Override<br /> private void method() {<br /> System.out.println("In child");<br /> }<br /> }<br />}<br /><br />It will give: "The method method() of type BaseClass.ChildClass must override or implement a supertype method"Anonymoushttps://www.blogger.com/profile/05269315121654881351noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-78662717946687147752015-10-24T12:31:37.356-07:002015-10-24T12:31:37.356-07:00that error will only occur it you use
@Override a...that error will only occur it you use <br />@Override annotationUnknownhttps://www.blogger.com/profile/15242146283043417750noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-80163768959141391022015-07-24T17:04:40.685-07:002015-07-24T17:04:40.685-07:00@mugume, I don't think that's true. You ca...@mugume, I don't think that's true. You can see in my example, I have same method name() in both super class and sub class. It's actually an example of method hiding than overloading. If a method is marked private, compiler won't bother to check super class. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-36984508115481537602015-07-24T17:02:35.359-07:002015-07-24T17:02:35.359-07:00@Sujit, since methods are invoked from constructor...@Sujit, since methods are invoked from constructor, they will automatically be called when you create an object of the class. You don't need to invoke any method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-47511232099995234922015-07-20T22:58:13.218-07:002015-07-20T22:58:13.218-07:00You have cretaed object only in above program....d...You have cretaed object only in above program....didn't invoke any method.Sujit Pingale....!!!https://www.blogger.com/profile/18431228242591748205noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-6701350259668588072014-05-22T05:08:25.406-07:002014-05-22T05:08:25.406-07:00Hello In this Program you are not getting any comp...Hello In this Program you are not getting any compile time error if you want you can check.@mugume david<br /><br />public class Prgoram2 {<br /><br /> /**<br /> * @param args<br /> */<br /> public static void main(String[] args) {<br /> // TODO Auto-generated method stub<br /> Derived d=new Derived();<br /> <br /><br /> }<br /><br />}<br />class BaseClass<br />{<br /> private void m1()<br /> {<br /> <br /> }<br />}<br />class Derived extends BaseClass<br />{<br /> private void m1()<br /> {<br /> <br /> }<br />}Anonymoushttps://www.blogger.com/profile/05904035274274930517noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53526972716529590102014-03-31T00:19:57.559-07:002014-03-31T00:19:57.559-07:00"Can we override a private method from super ..."Can we override a private method from super class into subclass? is that different than creating private method with same name in both super class and sub class in Java?"<br />-> Thats a compile time error. compiler wont find anything to override since it cant see the private methodAnonymoushttps://www.blogger.com/profile/09034936833223690555noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-83423637287483280282014-02-09T10:53:52.875-08:002014-02-09T10:53:52.875-08:00That's true.. It adds some more information to...That's true.. It adds some more information to this article.. :)Mr.Chowdaryhttps://www.blogger.com/profile/09039529137686327905noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-16816301166557878072013-09-03T06:23:53.769-07:002013-09-03T06:23:53.769-07:00Good Article, asked in two interviews consecutivel...Good Article, asked in two interviews consecutively. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-57764261874338310632013-06-27T00:20:28.107-07:002013-06-27T00:20:28.107-07:00Can we override a private method from super class ...Can we override a private method from super class into subclass? is that different than creating private method with same name in both super class and sub class in Java?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-18591903902345046422013-01-21T12:05:32.188-08:002013-01-21T12:05:32.188-08:00Useful information.
Thanks!Useful information.<br />Thanks!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-19068385536241550982012-12-27T05:13:25.670-08:002012-12-27T05:13:25.670-08:00Good this article is very useful for meGood this article is very useful for meUnknownhttps://www.blogger.com/profile/04412952141394522142noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-66076285148024508322012-08-30T07:24:48.033-07:002012-08-30T07:24:48.033-07:00Good points :
Private method is not inherited by s...Good points :<br />Private method is not inherited by subclass in Java. Sub class can not even see private methods of super class. when you declare private method in subclass no body knows about it except code in that class. That's why term method hiding is used. Accessing private method outside class is compile time error in Java.Anonymousnoreply@blogger.com