super keyword
i have a couple of question about the below code.
is it possible to call the SuperClass's printMethod from the main method in the subclass? Or can the SuperClass's printMethod only be called by using the super keyword from the subclass's printMethod?
class Superclass{
void printMethod(){
System.out.println("Printed in Superclass.");
}
}
//Here is a subclass, called Subclass, that overrides printMethod().
class Subclassextends Superclass{
void printMethod(){//overrides printMethod in Superclass.
super.printMethod();
System.out.println("Printed in Subclass.");
}
publicstaticvoid main(String[] args){
Subclass s =new Subclass();
s.printMethod();
}
}
[1444 byte] By [
mark_8206a] at [2007-11-27 2:39:38]

> is it possible to call the SuperClass's printMethod
> from the main method in the subclass?
No, because the main method is static and the printMethod method is not static. Trying to compile it would have told you that.
> Or can the
> SuperClass's printMethod only be called by using the
> super keyword from the subclass's printMethod?
"super" can be used in any non-static method.
You may have been trying to articulate this, it's hard to tell:
class Subclass extends Superclass {
public void anotherMethod() {
super.printMethod();
}
}
While you can call super.printMethod from other non-static methods in Subclass, I've never needed to that sort of thing.
you could call printMethod iff (that's if and only if)the method was declared static-or-you called the method within a non-static method in your subclass, and your subclass was in the same package as the parent class
> No, because the main method is static and the
> printMethod method is not static. Trying to compile
> it would have told you that.
yes it can be called thats what we call Dynamic Method Dispacth a.k.a Runtime Polymorphism
heres how
Superclass s1 = new Superclass();
s1.printMethod();
> > Or can the
> > SuperClass's printMethod only be called by using
> the
> > super keyword from the subclass's printMethod?
>
> "super" can be used in any non-static method.
here's another code run it
class dmdbase
{
int i;
dmdbase()
{
}
dmdbase(int k)
{
i=k;
}
void display()
{
System.out.println("the value of i" + i);
}
void show()
{
System.out.println("hello");
}
}
class dmd extends dmdbase
{
int j;
dmd(int e, int r)
{
super(r);
j=e;
}
void display()
{
System.out.println("the value of j is "+ j);
}
public static void main(String args[])
{
dmdbase b = new dmdbase(45);
dmdbase c = new dmd(85,99);
dmdbase d = new dmd(89,45);
b.display();
c.display();
d.show();
}
thanks for that code gaurav,but what's the purpose of the method that takes no parameters?dmdbase(){}
I wrote search engine in php that required a super or master keyword to get special results and I found that it was a loop hole for hackers.Somebody Help Pephi: http://hd1988.blogspot.com/Somebody Save Pephi: http://www.webbudd.co.za/tutorials.php
I just have an habbit of defining a parameterless constructorI guess theres no harm doing so
dmd(int e, int r)
{
super(r);
j=e;
}
i don't follow that method,
the line super(r);
can anyone please tell me what that does?
> > dmd(int e, int r)
> {
> super(r);
> j=e;
> }
>
>
> i don't follow that method,
> the line super(r);
> can anyone please tell me what that does?
super(r)
calls the superclass constructor and passes the value r to it as argument.
Also remember that this line should be the first line the subclasses constructor body
please read the java language tutorial thouroughly