GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract classes" Watch "Abstract classes" New topic
Author

Abstract classes

Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi all..
pls tell me how is the following statement true :
An abstract method cannot be private, static, final, native or synchronized.
i understand the private and final parts, but what abt the rest??
pls help..
thanx..
------------------
Hima


Hima<BR>Sun Certified Java Programmer
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi hima,
static method's cannot be overridden if u make your abstract method as static then u won't be able to override that abstract method in subclass.The abstract method are not complete therefore any subclass which has that abstarct method has to provide an implementation of that method.Native method's does not have a body.if u override an abstarct method which is native as well then u have to provide body to that method in the subclass, native say's that u cannot have a body of a method.

Sanjiv Pillai
Greenhorn

Joined: Mar 29, 2001
Posts: 4
Hi Nitin,
Static methods can definitely be overridden but only by static methods. I think the reason abstract methods can't be static is that then they can be referenced (called) directly without instantiation of the class.
eg. if "method1()" is abstract & is allowed to be static then it can be called directly as follows "Classname.method1()" & since it doesn't have any body it doesn't make any sense. i guess that is the reason.
i guess Hima that also answers ur query.
cheers sanjiv
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
class lapore
{
static void lapore()
{
System.out.println("lapore method of the base class");
}
}
class lap extends lapore
{
static void lapore()
{
System.out.println("lapore method of the derived class");
}
public static void main(String[]args)
{
lapore l=new lap();
l.lapore();
}
}
hi sanjeev,
static method's cannot be overridden is true.I can prove that, just see the code given above,i have s attaic method in the lapore class and i have provided the same method in the derived class(lap).
Now come inside the main function,i have created an object of the lap class whose reference has been assigned to the reference variable of class lap.when i call a method lapore it call's the method of the base class(lapore) not of that derived(lap).Had the lapore method been overridden in the derived class (lap) then it would have called the method of derived class (lap) rather than that of the base class(lapore),reason being static method's are resolved at compile time therefore it is the type of reference varaible which decide's which method to call not the actual object of the class.
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi Nitin,
I tend to agree with Sanjiv. The example you have given
certainly demonstrates that static methods are resolved at compile time. But that does not mean static methods cannot be
overridden. If we changed the code in the example such that
lap l=new lap();
l.lapore();
then the overridden method for the subclass would be called
isnt it ?
The question posed in the beginning was whether an abstract method can be static or not. As Sanjiv has mentioned, if it were allowed to be abstract then you could call the method without creating an instance. But there would be no method body available to execute that code since the method is abstract.
Do provide your comments.
Sajida
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi sajid,
that is not overrididng , that is just another method with the same name as in the base class(lapore).sir, it is the type of object that decide's which method to call.In my code a method with the same name is there in both the classes.
lap l=new lap().
the above statement will definitely work becusae here i have created an object of lap so it will call the method of lap.But,
lapore l=new lap(); //
the above given line should have called the method from lap, if it's overriding then the method of lap should have been not of that lapore.That is not overriding.

class lapore
{
public void lapore()
{
System.out.println("lapore method of the base class");
}
}
public class lap extends lapore
{
public void lapore()
{
System.out.println("lapore method of the deived class");
}
public static void main(String[]args)
{
lapore l=new lap();
l.lapore();
}
}
sajid , look at the above given code,same code just removed the static modifier from infront of the method's.
Now it's calling the method of the lap class not that of the lapore class becuase here the late binding come's into picture.method's are resolved after the object get's created.
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi guys..
thanx for ur replies.. although the conversation did digress a bit from what i intended to ask.. :-).. but i did get a fair idea of why abstract methods can't be native and static.. i still don't understand the synchronized bit though..
hope somebody answers that too..
------------------
Hima
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
abstract class ab
{
static abstract void add();
}
public class vote extends ab
{
public static void main(String[]args)
{
add();
}
}
hi sajid and sanjeev,
This is what u meant to say in your previous posting's that somebody may call an abstract method directly in the subclass which does not make sense.
Now think,Had static modifier with abstract method been allowed, u would still not be able to access that method in your subclass because a method without a body still has to be declared abstract,beside's that u would have to declare your class an abstract one.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi hima, sajid and sanjeev, read below
Q. Can subclass override methods that are declared static in the superclass?
A: No!
"Methods a Subclass Cannot Override
A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message similar to the following and refuses to compile the program:"
...
"Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass. "
Quotation from Overriding Methods of The Java Tutorial
An interesting example here:
class Base {
static void doThis() {
System.out.println("Base static doThis()");
}
void doThat() {
System.out.println("Base instance doThat()");
}
}
class Sub extends Base {
// This is static method defined in the Sub class
// which does not override the Base doThis()
// only hide it.
// proved in the Test.main()
static void doThis() {
System.out.println("Sub static doThis()");
}
// This is instance method defined in the Sub class
// which does override the Base doThat()
void doThat() {
System.out.println("Sub instance doThat()");
}
}
public class Test {
public static void main(String[] args) {
// base is an instance of Sub
Base base = new Sub();
// legal cast both at compile and run time
Sub sub = (Sub)base;

base.doThis(); //Base static doThis()
base.doThat(); //Sub instance doThat()

sub.doThis(); //Sub static doThis()
sub.doThat(); //Sub instance doThat()

//Think why, try to understand the following concepts
//static binding : bind at compile time
//Dynamic binding : bind at run time
}
}

To the top
--------------------------------------------------------------------------------
Q. Why the call to xy.amethod() calls amethod() in X instead of amethod() in Y in the following code?
class X {
public static void jout(String s) {
System.out.println(s);
}
static void amethod() {
jout("In X, amethod()");
}
}
class Y extends X {
static void amethod() {
jout("In Y, amethod()");
}
}
class Z extends Y {
static void amethod() {
jout("In Z. amethod()");
}
}
public class Test
public static void main (String[] args) {
X xy = new Y();
X xz = new Z();
Y yz = new Z();
xy.amethod();
xz.amethod();
yz.amethod();
}
}

A:
All three amethod()s here are static methods. static method is Class method, which has nothing to do with your object instantiation, dynamic binding, or polymorphism, whatsoever.
In other words, the 3 line of code
xy.amethod();
xz.amethod();
yz.amethod();
can be replaced by the following 3 lines without causing any changes:
X.amethod();
X.amethod();
Y.amethod();
Since compiler interprets them exactly like this. xy and xz are reference type of X. yz is reference type of Y.
Just remember "static is not dynamic" probably will help some.
To the top
--------------------------------------------------------------------------------
Q. When base class calls a method, how could the run time knows to called the overridden method in the derived class? The object is not even constructed yet.
A:
Why? From the language theory, it is called deferred binding or dynamic binding or binding at run time. Compiler leaves the decision to run time. To make the decision at run time, you don't need the object built first. The only thing you need to know is the type of the actual object.
In Java, the Class (not class) object is load to the memory when it is first used. The run time knows it before any instance is constructed. In C++, the class methods virtual table is there for the same purpose. As you probably know, in C++, only virtual methods are dynamically bound.
To the top
--------------------------------------------------------------------------------
Q. Does private method defined in base class participate polymorphism, if the derived class has a same name method?
A: No, private method defined in any class will never participate polymorphism.
If you want to confuse yourself, your boss, and your co-worker (and you might get fired for that too), and define same name private methods along the hierarchy, the compiler will treat them individually, as no relationship at all. If you call private method in your class, the method will be bound at compile time (not runtime).
See the following example:
// Base.java
//*******************************
public class Base {
public static void main(String[] args) {
Sub sub = new Sub();
System.out.println(sub.g()); // 20, not -100
}

private int f() {
return 20;
}

protected int g() {
// statc bound with Base.f()
// compiler does not know any derived class has f() or not
// and does not care either
return f();
}
}
class Sub extends Base {
// f() here has nothing to do with Base.f()!!!
// In real world programming, never do this!!!
public int f() {
return -100;
}
}


There is a bit of extra information u can leave that and take the relevant material
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract classes