Abstract class in Java

后端 未结 14 1290
孤独总比滥情好
孤独总比滥情好 2020-11-22 12:32

What is an \"abstract class\" in Java?

相关标签:
14条回答
  • 2020-11-22 13:25

    Solution - base class (abstract)

    public abstract class Place {
    
    String Name;
    String Postcode;
    String County;
    String Area;
    
    Place () {
    
            }
    
    public static Place make(String Incoming) {
            if (Incoming.length() < 61) return (null);
    
            String Name = (Incoming.substring(4,26)).trim();
            String County = (Incoming.substring(27,48)).trim();
            String Postcode = (Incoming.substring(48,61)).trim();
            String Area = (Incoming.substring(61)).trim();
    
            Place created;
            if (Name.equalsIgnoreCase(Area)) {
                    created = new Area(Area,County,Postcode);
            } else {
                    created = new District(Name,County,Postcode,Area);
            }
            return (created);
            }
    
    public String getName() {
            return (Name);
            }
    
    public String getPostcode() {
            return (Postcode);
            }
    
    public String getCounty() {
            return (County);
            }
    
    public abstract String getArea();
    
    }
    
    0 讨论(0)
  • 2020-11-22 13:27

    From oracle documentation

    Abstract Methods and Classes:

    An abstract class is a class that is declared abstract—it may or may not include abstract methods

    Abstract classes cannot be instantiated, but they can be subclassed

    An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

    abstract void moveTo(double deltaX, double deltaY);
    

    If a class includes abstract methods, then the class itself must be declared abstract, as in:

    public abstract class GraphicObject {
       // declare fields
       // declare nonabstract methods
       abstract void draw();
    }
    

    When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

    Since abstract classes and interfaces are related, have a look at below SE questions:

    What is the difference between an interface and abstract class?

    How should I have explained the difference between an Interface and an Abstract class?

    0 讨论(0)
提交回复
热议问题