java abstract class (abstract)

(A) When it comes to abstract classes, then what is the abstract class it? Example of a life to find out. For example, a parent to name the graphic , then his sub-classes may be square, triangle, circle, etc., then like this subclass their area can find out through the formula. Now give you a question so that you find the parent class graphics area
1. First thing clear graphics called the parent class is an area of
2. You just do not know what graphics, so we say that the parent class is abstract graphic
3 DETAILED pattern. subclass is given a specific class

(B) the
abstract class: abstract class where the method must be abstract class because as written before class on abstract;
abstract methods: abstract is to add keywords, then remove the braces, ending direct semicolon;
how to use abstract methods and abstract class does
1. not directly create new abstract objects
2. must inherit the abstract parent class with a subclass
3. subclass must override override all abstract methods abstract parent class among (remove the abstract make up braces)
4 It was performed using subclassing objects using

(C) below with an example,
the definition of an abstract class Animal (animal), defines two abstract methods eat (to eat) and sleep (sleep).

package edu.edu.shengda3;

public abstract class Animal {     //抽象类
public abstract void eat();             //抽象方法1

public abstract void sleep();         //抽象方法2
}

Normally, this time to pay attention to the need to define a specific class to inherit the abstract class, if I have chosen not to do that, this time you can come an abstract subclass of Dog (dog) to inherit the parent class Animal, replication subclass the parent class method

package edu.edu.shengda3;

public abstract class Dog extends Animal {
public void eat(){
    System.out.println("狗吃骨头");
}
}

To this point some people will say it is not a subclass must inherit the parent class method, in order to run it successfully? It is indeed the case, but we can continue to go into it. Relative to the Dog class, we come to two specific sub-categories, and second, Kazakhstan and golden. The Dog has become abstract parent class, two golden and Kazakhstan are two concrete classes.
Two Kazakhstan:

package edu.edu.shengda3;

public class Erha extends Dog {
public void sleep(){
    System.out.println("呼呼大睡");
}
}

Golden:

package edu.edu.shengda3;

public class Jinmao extends Dog {
public void sleep(){
    System.out.println("嘿嘿大睡");
}
 }

Now all of the classes are defined over to a main method just mentioned new abstract class can not be directly at the first point, we can only start from a specific category and two Golden Ha:

package edu.edu.shengda3;

public class demomain {
public static void main(String[] args) {
    Erha e=new Erha();
    e.eat();
    e.sleep();
    System.out.println("=========");
    Jinmao j=new Jinmao();
    j.eat();
    j.sleep();
}
 }

Output:

Here Insert Picture Description
Again:
1. abstract class can not create objects
Animal AN = new new Animal ();
Dog d = new new Dog (); // this is wrong
2. class does not have to draw an abstract way, but some are abstract class,
as shown in the code Dog
subclass of abstract class 3 must override any methods of the parent class, unless the subclass is an abstract class, no code written as Dog Animal in all methods, but for two and Ha Golden this particular class must override any methods.

Published 15 original articles · won praise 4 · Views 332

Guess you like

Origin blog.csdn.net/falaoxx/article/details/104649568