• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic