Not everything that counts can be counted, and not everything that can be counted counts.
1. Does a static method gets inherited to it's sub classes ?
Yes
SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Originally posted by rathi ji:
No, static method doesn't get inherited to it's sub classes.
Originally posted by Arvind Sampath:
2. Does the private method gets inherited to the sub classes ?
No. Private methods are private to the classes in which they are defined. They are not accessible anywhere outside.
3. If the private method is not overridden in the sub class, can we
invoke the private method of the Base class thru sub class object ?
ie sub.supA(); // is it allowed ?
No. Private methods are not inherited by a sub class. Hence, they cannot be overridden neither can they can be accessed using an object of the subclass. Infact, they cannot be accessed outside the class in which they are defined even by using the object of the corresponding class
![]()
Arvind[/QB]
Not everything that counts can be counted, and not everything that can be counted counts.
If the private methods are not inherited to the subclasses, why does the concept of overriding rules comes into picture like "we can broaden the method scope in the sub classes"
ie if "private" in super class, we can make it "public" in the sub classes ? In fact we are not overriding the method, right ?
static methods cannot be overridden to a lesser level of visibility
A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.
A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.
Static method can be inherited
example below
public class Test1
{
public static void testMethod()
{
System.out.println("Testing");
}
}
public class Test2 extends Test1
{
public static void main(String[] args)
{
Test2 test2 = new Test2();
test2.testMethod();
}
}
Regards,<br />Seb<br /> <br />SCJP 1.4
Originally posted by Sasikanth Malladi:
is that static methods cannot participate in polymorphism.
Am I right?
Sashi
class Super {
public static void testMe() {
System.out.println("I am in Super");
}
}
class Sub extends Super {
public static void testMe() {
System.out.println("I am in Sub");
}
}
public class Test{
public static void main(String args[]) {
Super sup = new Sub();
sup.testMe();
}
}
SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
"I'm not back." - Bill Harding, Twister
Raghusham Sankargal
SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Cheers,<br />KicKy
SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Originally posted by Jeff Kumar:
Kicky San:
1. static methods can be inherited
>> Right
Inheritance means, getting super class's method in sub class, adding specific behaviour in sub class if we need, calling super class's method from that with super keyword...
Originally posted by Kicky San:
From this thread and by the following example, I am jotting down my observations. Please correct me if i am wrong.![]()
1. static methods can be inherited
2. static methods can be called both by the class name and the class instance
3. static methods cant be overridden. static means one in a class.
4. Even if there is a static method in the child class same as the one in the parent class, it is not the overriden method. It is a seperate method of the child class.
Hope this example will clear all these points.
I have modified the previous example. Here is the modified version
class InheritStaticBase
{
public static void print()
{
System.out.println("I am in base class");
}
}
class InheritStaticChild extends InheritStaticBase
{
public static void print()
{
System.out.println("I am in child class");
}
}
class Main
{
public static void main(String args[])
{
System.out.println("using parent class name");
InheritStaticBase.print();
System.out.println("using child class name");
InheritStaticChild.print();
System.out.println("befre instantiating child class");
InheritStaticChild a = new InheritStaticChild();
a.print();
System.out.println("after instantiating child class");
System.out.println("befre instantiating parent class");
InheritStaticBase b = new InheritStaticBase();
b.print();
System.out.println("befre instantiating parent class");
}
}
Not everything that counts can be counted, and not everything that can be counted counts.
Originally posted by Enge Chall:
Hi Kicky, thank you for the explanation. But according to your theory :
Does it mean that if I have a main() method( ie entry point for execution ) in the Base class,
don't I need to write one more main() method in the sub classes (say I need same logic of main() method for sub classes also) ??? ie
If I run the prog say: java Sub
It must invoke the Superclass main() method if I don't have a main() in the sub class... am I right ?
SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
you need to pass the scjp again or improve yourself to know that the static method can be called by both the class name and instance name.
you need to pass the scjp again or improve yourself to know that the static method can be called by both the class name and instance name.
Regards,<br />Seb<br /> <br />SCJP 1.4
Not everything that counts can be counted, and not everything that can be counted counts.
Originally posted by Sasikanth Malladi:
Tim, here's what you said:
Originally posted by Ravisekhar Kovuru:
Does it mean that even if we call static methods on instance, the JVM calls them on Class name???
Can someone clarify this please..
[/QB]
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|