What is the use of writing a private method inside an abstract class, and can we write public static inside that class? Please give an example.
You can use any kind of method in an abstract class. The only difference between an abstract class and a normal one is that the abstract class contains methods which have no body:
public abstract Foo {
public void foo() {
bar();
}
private void bar() {
doSomething();
}
protected abstract void doSomething();
}
So while bar()
has no idea what doSomething()
really does, it knows that it will exist eventually and how to call it.
This is enough for the compiler to create the byte code for the class.
we can have our implementation in abstract class and so the private method
For Example :
public abstract class AbstractDAO{
public void save(){
validate();
//save
}
private void validate(){ // we are hiding this method
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With