K
kaja_love160
hello
1)
If method ( call it U ) in parent class is made private, then methods
of child class can’t access U directly, but they still inherit this
private method U.
Is it similar situation if parent method ( call it U1 ) is declared
final ïƒ Meaning child methods can’t access U1 directly, but they still
inherit it?
2)
Subclass can override parent’s class method by creating method with
same name and same parameter list --> this is not true if parent
method is declared private
So if compiler identifies a method by its name and its parameter list,
then why do we get compile-time error if “hey()†in B is private
( compiler informing us that “hey()†can’t be overriden ):
class A{
private final void hey(){…}
}
class B extends A{
private void hey(){…}
}
, while compiler doesn't complain when “hey()†in class B is made
public:
class A{
private final void hey(){…}
}
class B extends A{
public void hey(){…}
}
? That would suggest that when it comes to overriding method, java
doesn’t only distinguish between methods by their name and parameter
list, but also by their access modifier.
thank you
1)
If method ( call it U ) in parent class is made private, then methods
of child class can’t access U directly, but they still inherit this
private method U.
Is it similar situation if parent method ( call it U1 ) is declared
final ïƒ Meaning child methods can’t access U1 directly, but they still
inherit it?
2)
Subclass can override parent’s class method by creating method with
same name and same parameter list --> this is not true if parent
method is declared private
So if compiler identifies a method by its name and its parameter list,
then why do we get compile-time error if “hey()†in B is private
( compiler informing us that “hey()†can’t be overriden ):
class A{
private final void hey(){…}
}
class B extends A{
private void hey(){…}
}
, while compiler doesn't complain when “hey()†in class B is made
public:
class A{
private final void hey(){…}
}
class B extends A{
public void hey(){…}
}
? That would suggest that when it comes to overriding method, java
doesn’t only distinguish between methods by their name and parameter
list, but also by their access modifier.
thank you