Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of private method inside abstract class [duplicate]

Tags:

java

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.

like image 899
sandeep Avatar asked Dec 21 '22 15:12

sandeep


2 Answers

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.

like image 136
Aaron Digulla Avatar answered Jan 13 '23 23:01

Aaron Digulla


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
  }

}
like image 43
jmj Avatar answered Jan 13 '23 22:01

jmj