• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

when can you override

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi again,
i found this question

55. When can't you override a method?

. when method is declared abstract

A. When method is declared final

B. when method is declared private

C. when method is declared static

i answered A,B,C.... exam says only B is right.....

anyone could help?

thanks and regards
marco
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think A should also be the answer.becasue final methods cannot be overriden.

and ofcourse private methods.
reagrding static methods , i think they cnanot be overriden to be non-static, else they can be overriden., but i am sure , i am alsor eading about it.
correct me if i am wrong.
 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry , i was wrong.static method cannot be overriden.page 102 s&b.
thanks
 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
once again i tested the point for static.
class TestStaticParent {


public static void methodA(){
System.out.println("parent method");
}





}

class TestStaticChild extends TestStaticParent {
public static void methodA(){
System.out.println("child method");
}

public static void main(String [] args) {

//TestStaticParent p = new TestStaticParent();
//TestStaticChild c = new TestStaticChild();
TestStaticParent.methodA();
TestStaticChild.methodA();
}



}

it worked, so we can override static methods , but we cannot override to be non-static.
thanks
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure... but i think the question is not distinguishing between overridden and hiding,

only final method cannot be hidden or overridden.

static and pirvate methods get hidden but cannot be overridden
 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vishal,

my program did not crib about overrding static method.
please correct me if anything is wrong in the program.
from the program i concluded that static methods can be overriden.
but we cannot override them to be nonstatic.
thanks
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give more about the static methods regarding the warning issued but the compiler for the following example

Static methods can declared with the same signature in the subclass and its not overriding but method hiding, they say so.
Consider the following code


This runs and produces output as
Inside Child method


What does the warning imply

  • Is the methods can't be overridden since the signature is not equal
  • So it means if signatures are equal then its overridden
  • Doesn't mention anything about method hiding



  • Final methods can't be overridden
    Private method are not even visible in the scope for the inherited class so overriding doesn't apply.
     
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by sirisha sirisha:
    ...from the program i concluded that static methods can be overriden...


    No, static methods cannot be overridden, but they can be hidden. See Overriding vs. Hiding.
     
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    1) abstract method has to overriden (for specific use).

    2) final methods cannot be overriden at any cost.final methods are not inherited.

    3) private methods are not inherited so no Question of overriding.
    (private methods are implicitly final).

    4) static method cannot be overriden thay can only be hidden by method with similer
    prototype in subclass(But same overriding rules applies-so ppl think static method can also be overriden).

    I have written a article on this topic let me know ur email-id if you want.
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    5) final and private methods are never inherited into subclass when extended.

    6) where as abstract and static methods are inherited.

    7) static method can never be overriden.static method in superclass is hidden in the subclass if thier is another static
    method with same prototype exists in subclass.(then method in superclass can be accessed in subclass only by using super keyword).
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    5) final and private methods are never inherited into subclass when extended.

    6) where as abstract and static methods are inherited.

    7) static method can never be overriden.static method in superclass is hidden in the subclass if thier is another static
    method with same prototype exists in subclass.(then method in superclass can be accessed in subclass only by using super keyword).
     
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Girish,

    I got ur point.
    But at the time of calling which one is called super/sub class

    Super -> SuperClass
    Sub -> SubClass

    1) Super s1=new Sub();

    2) Sub s2=new Sub();


    I think in case1 the staticmethod in SuperClass is called
    in case2 the one in SubClass is called.

    Am i Right??

    Naveen.
     
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Girish Nagaraj:

    2) final methods cannot be overriden at any cost.final methods are not inherited.


    Final methods are not inherited by subclasses? That is incorrect.
     
    M Mistroni
    Ranch Hand
    Posts: 66
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Girish, my email is mmistroni@gmail.com could you send me that article you mentioned?

    thanks and regards
    marco
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Eimers,

    Final methods are not inherited by subclasses? That is incorrect.

    Yup! you are right.Any how thanks for idetifying my mistake.
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Super -> SuperClass
    Sub -> SubClass

    1) Super s1=new Sub();

    2) Sub s2=new Sub();


    I think in case1 the staticmethod in SuperClass is called
    in case2 the one in SubClass is called.

    Am i Right??

    You are right.

    When the method is static, method that is called depends on the declared type of the reference not the object it is currently pointing to.
     
    Girish Nagaraj
    Ranch Hand
    Posts: 153
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Eimers,

    Final methods are not inherited by subclasses? That is incorrect.

    I agree that you are right.Final methods can be inherited.

    In khalid mughal java certification book he has given

    "private methods are implicitly final".What sense does it makes.......

    Don't you think declaring private methods as final is of no use???

    bcoz private methods are any how not visible in the subclass.Then where is the question of overriding.

    class Base {

    // What diff. does it make if I don't add final.
    final private void method() {

    System.out.println("Base:method()");
    }
    }

    public class Test24 extends Base {

    public static void main(String[] args) {

    Test24 instance = new Test24();

    instance.call();
    }

    void call() {

    method();
    }

    void method() {

    System.out.println("Extended:method()");
    }
    }
     
    And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic