• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding.  RSS feed

 
edi kapllani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we got this example:
class A{
private void a(){}
}
abstract class B extends A{
abstract protected void a();
}
class C extends B{
public void a(){}
}
does method a in class C overrides method a in class A ?
thank you!
ciao!

------------------
edi kapllani
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the method in C doed not override the method in A. Actually since the method in A is declared as privete, neither classes B or C is even aware of it.

Bosun
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edi,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
Thanks,
Cindy
 
Tim Leung
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public abstract class A{
public void a();
}
class B extends A{

public void a(){
//anykind of method
}
}

can method a in abstrct class A overrides method a in class B in this case?
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
Whenever you have a abstract method in a class you have to declare it as abstract. So your class A defition look like this,

public abstract class A{
public abstract void a();
}

In that case the 'a' method is overriden in class B.

-Manju
[ September 06, 2004: Message edited by: Manjunath Subramanian ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, Tim doesn't have to declare method a() in abstract class A as abstract.

You can't have a concrete class containing an abstract method, but you can have an abstract class containing a concrete method.
[ September 06, 2004: Message edited by: Mike Gershman ]
 
Tim Leung
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear mike.

As you said, all method in abstract class and its subclass (may be a class or abstract class) must contains override method.is it correct?

abstract class A{
abstract void methodA();
}

class B extends A{
void methodA(){
}
}

since methodA in class B must be used. How about interface?

interface A{
void methodA();
}

class B implements A{
void methodA(){
}
}
Is it also override?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your examples of overriding methods are both correct. In each case, class B needed an implementation of MethodA.

However, let me modify your first example:



class B does not require its own implementation of MethodB because it inherits a concrete implementation of methodB from class A.

BTW, try using the code tags, they preserve the indentation of your examples for readability.
 
Abhi Dhanda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Manju's example is 100% correct as -


b'coz Tim havn't given the body tag's to method a().

Although you are also correct as -

You can't have a concrete class containing an abstract method, but you can have an abstract class containing a concrete method.


-Abhi.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!