• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

static methods

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i have doubt in static methods, as static method can't be overriden,how is it possible to access the behaviour of super class in base class.i have code
class Animal
{
static void dostuff(){
System.out.println("aaaaaa");
}
}

class Dog extends Animal
{
public static void main(String[] args){
//Animal a=new Animal();
//a.dostuff();
//Animal a1=new Dog();
//a1.dostuff();
Dog d=new Dog();
d.dostuff();
}
}

though i now static methods are not overriden, as they method and can be accessed in base class through hidding ,but here i not used the static method then i able to access it .if i am wrong please guide me.
thankyou,
trupti.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi trupti!!!


Accessing a static method of super class using inheritance
is a different thing,and overriding it are two different concepts
here you are accesssing static method of super class but you try
to override it,it is not posssible to override them..........

can try the below example.............





Now you run this program the ouput will be Doooogggg
and not aaaaaaaaaa,bcoz static methods cannot be overriden......


i hope it helps..............
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
trupti,

overriding and hiding concept comes into picture if and only if we have inherited behavior.


1. if we have any non-static inherited behavior from Parent, child
can override that.

2. if we have any static inherited behavior from Parent, child
can hide it.



The rules for over-riding and hiding are same.
 
bacon. tiny ad:
ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
reply
    Bookmark Topic Watch Topic
  • New Topic