Learning Java - abstract class, an abstract method (abstract) (with examples)

This article will describe the knowledge of java abstract classes and abstract methods, this is the simplest and most easily forgotten. In Java, if you want to represent an abstract class or method, we need to use the abstract, its Chinese translation is "abstract."

Abstract classes and abstract methods

A: sort of knowledge

1. Basic concepts:

In object-oriented concept, all objects are described by the class, but it does not mean that all classes are used to describe the object, if a class does not contain enough information to describe a specific object, this class is an abstract class.
Example: Shape is an abstract concept. The method of calculating the shape of the different subclasses of the area is not the same. It provides
abstract methods to be implemented by different subclasses.

//抽象类Shape
abstract public class Shape{
       abstract double area();//抽象方法
}

As can be seen from the example, the abstract is the keyword == abstract == modified. Abstract class has a special way,
i.e. by keyword == abstract == modified method, referred to as "abstract method."

2. abstract classes and abstract methods declared format:

Here Insert Picture Description

3. The method of abstract classes and abstract features:

(1) not allowed to instantiate abstract method, in other words can not create object abstract class, it is only as a parent of other classes. However, the transformation can be upwardly directed instantiated.
(2) only abstract method declarations, there can be achieved, that is only the first method, but there is no way to achieve body and operational.
Such as: abstract double area ();

4. Significance abstract class that defines:

(1) providing for a common type of subclass (subclass references to the parent class object);
(2) duplicates (member variables and methods) package subclass; and
(3) are designed to abstract parent class after , either by a parent-child inheritance design constraints arbitrary subclass, to some extent, avoids the parent class is instantiated meaningless

Focus attention

contains the abstract class methods, can be defined as an abstract class
as described below, is not defined as an abstract class will be given when:

Here Insert Picture Description
The correct code is:

Here Insert Picture Description
abstract class does not necessarily contain abstract methods.
example

abstract public class Shape{
    public void girth(){
        System.out.println("圆形周长为....");//一般方法
    }        
}

member methods in an abstract class may include general methods and abstract methods

abstract public class Shape{
    public void girth(){
        System.out.println("圆形周长为....");//一般方法
    }
         abstract double area();//抽象方法
}

abstract class can not be instantiated, even if the abstract class does not contain an abstract method, the abstract class instance can not be created. Abstract class constructor is invoked primarily for its subclasses.
Examples:
the Shape abstract class is not an abstract method comprising:

abstract public class Shape{
    public void girth(){
        System.out.println("圆形周长为....");//一般方法
    }       
}

Test class instances of Shape, the compiler will complain:
Here Insert Picture Description

After a class inherits the abstract class must implement all abstract methods, or is an abstract class, different subclasses can have different implementations of the abstract methods of the parent class.

//抽象类父类Shape
abstract public class shape{
     abstract double area();//抽象方法
}

Then its subclasses Circle There are two approaches:

Scheme I: abstract method override area (), a method is achieved

//字类圆Circle类
public class Circle extends shape{
     //属性:圆的半径r
     public double r;
     public Circle(double r){
            this.r=r;
   }
   //重写父类中area()方法
   public double area(){
          return(double)(3.14*r*r)
  }
}

Option II: subclass of Circle class also defined as an abstract class

public abstract class Circle extends Shape{
       //属性:圆的半径r
      public double r;
      public Circle(double r){
      this.r=r;
      }
}

even if the parent class is concrete, but it may also be a subclass of abstract. Such as Object is concrete, but can create an abstract subclass.
abstract method can not be modified with static and private; for the class, with the final and abstract can not be modified, because the final keyword makes the class can not be inherited, and modified abstract class can not inherit if would not make sense. The two together, would conflict

As usage will cause a compiler error:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

A complete and accurate abstract class example

Declare an abstract class Shape, abstract member method area (). Shape two derived classes and subclasses round rectangle Rectangle Circle class. Shape in the abstract method declarations area (), which are realized in two subclasses.
Code is as follows:
abstract class parent class Shape

//抽象类父类Shape
abstract public class Shape{
  abstract double area();//抽象方法
}

Subclass circle Circle

//子类圆Circle
public class Circle extends Shape{
    //属性:圆的半径r
    public double r;

    Circle(){
    }
    //创建带参构造函数(参数为r)
    public Circle (double r){
        this.r=r;
   }

   public double getR(){
        return r;
   }
   public void setR(double r){
        this.r=r;
    }
    //重写父类中area()方法
    public double area (){
           return(double)(3.14*r*r);
    }    
}

Subclasses class Rectangle rectangle

//子类矩形类Rectangle
public class Rectangle extends Shape{
     //属性:矩形的长length、宽wide
     public double length;
     public double wide;

     Rectangle(){
     }
     //创建带参构造函数(以length和wide为参数)
     public Rectangle(double length,double wide){
         this.length=length;
         this.wide=wide;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public double getWide() {
        return wide;
    }
    public void setWide(double wide) {
        this.wide = wide;
    }
    //重写父类的area()方法
    public double area(){
        return length*wide; 
    }   
}

Test category

//测试类
public class text {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Circle c=new Circle(3.5);
        Rectangle re=new Rectangle(6,5);
        //调用area()方法,输出结果
        System.out.print("圆的面积为"+c.area());
        System.out.print("矩形的面积为"+re.area());
    }

}

Guess you like

Origin blog.51cto.com/13955869/2422039