What is the purpose / use of abstract classes? (Look for examples in the real world.)

Can someone show me an example of an abstract class in Java? Something using the real world (rather than a sample textbook) would be desirable.

Thank!

+3
source share
5 answers

Read this tutorial on Abstract Methods and Classes .

, GraphicObject, , , , moveTo . GraphicObject , , , -. GraphicObject - :

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
    abstract void resize();
}

GraphicObject, Circle :

class Circle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}
class Rectangle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}
+6

, java.util.AbstractCollection?

+3

AbstractTableModel. AbstractTableModel TableModel ( JTable). TableModel :

public boolean isCellEditable(int rowIndex, int columnIndex);
public Class<?> getColumnClass(int columnIndex);
public int getColumnCount();
public int getRowCount();
public Object getValueAt(int rowIndex, int columnIndex);
public String getColumnName(int columnIndex);
public void addTableModelListener(TableModelListener l);
public void removeTableModelListener(TableModelListener l);
public void setValueAt(Object aValue, int rowIndex, int columnIndex);

AbstractTableModel , (getRowCount, getColumnCount getValueAt), exteding . , .

+2

Java abstract classes , .
, ( ). . , ( ). , .

.

abstract class A {
         public abstract abs_value();

         void show() 
                   {
                   System.out.println("This is an abstract class");
                   }
               }
+2

, :

, , , ORM, ​​ Hibernate ( , , , / ).

, , (DAO) , , DAO , . AbstractBaseDao, , DAO:

public class AbstractBaseDao {
    public void saveOrUpdate(Object o){
         //save or update object from database
    }

    public void loadAll(Class clazz){
        //Load all from database
    }

    public Object loadById(Class clazz, Long id){
       // Retrieve object by Id from database
    }

    // Additional common methods
}

, , , , DAO , .

BaseDao DAO , - :

public class PersonDao extends AbstractBaseDao{
     public List<People> listAllPersonsByLastName(String lastName){
         //Load people by last names from database
     }

     public List<People> listAllPersonsByBirthYear(Date date){
        //List people by birth year from database
     }
}

PersonDao , , , . .

, , , , .

+1

Source: https://habr.com/ru/post/1752195/


All Articles