Java abstract class

Source: Internet
Author: User
Tags define abstract

In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.

Because an abstract class cannot instantiate an object, an abstract class must be inherited in order to be used. For this reason, it is often decided at the design stage to not design abstract classes.

The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.

In Java, an abstract class represents an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.
Abstract class

The

uses abstract class in the Java language to define abstract classes. The following example:
Employee.java file code:
/* File name: Employee.java */
Public abstract class Employee
{
Private String name;
Private String address;
private int number;
Public Employee (string name, address of string, int number)
{
System.out.println ("Constructing an Employee");
THIS.name = name;
This.address = address;
This.number = number;
}
Public Double Computepay ()
{
System.out.println ("Inside Employee computepay");
return 0.0;
}
public void MailCheck ()
{
System.out.println ("Mailing a check to" + THIS.name
+ "" + this.address);

Public String toString ()
{
return name + "" + Address + "" + number;
}
Public String getName ()
{
return name;
}
Public String getaddress ()
{
return address;
}
public void setaddress (String newaddress)
{
address = newaddress;
}
public int GetNumber ()
{
return number;
}
}

Notice that the employee class is not different, although the class is abstract, but it still has 3 member variables, 7 member methods, and a constructor method. Now if you try the following example:
Abstractdemo.java File Code:
/* File name: Abstractdemo.java /
public class Abstractdemo
{
public static void Main (String [] args)
{
/
The following are not allowed and will cause an error */
Employee E = new Employee ("George W.", "Houston, TX", 43);

  System.out.println("\n Call mailCheck using Employee reference--");  e.mailCheck();}

}

When you try to compile the Abstractdemo class, the following error is generated:

Employee.java:46:employee is abstract; Cannot be instantiated
Employee E = new Employee ("George W.", "Houston, TX", 43);
^
1 Error

Inheriting abstract classes

We can inherit the Employee class in a general way:
Salary.java file code:
/* File name: Salary.java */
public class Salary extends Employee
{ Br>private double salary; Annual salary
Public salary (string name, string address, int number, double
salary)
{
Super (name, address , number);
Setsalary (Salary);
}
public void MailCheck ()
{
System.out.println ("Within MailCheck of Salary class");
System.out.println ("Mailing Check to" + getName ()
+ "with salary" + salary);
}
Public Double getsalary ()
{
return salary;
}
public void Setsalary (double newsalary)
{
if (newsalary >= 0.0)
{
salary = newsalary;
}
}
Public Double Computepay ()
{
System.out.println ("Computing salary pay for" + getName ());
return SALARY/52;
}
}

Although we cannot instantiate an object of an employee class, if we instantiate a salary class object, the object inherits 7 member methods from the employee class, and the method allows you to set or get three member variables.
Abstractdemo.java File Code:
/* File name: Abstractdemo.java */
public class Abstractdemo
{
public static void Main (String [] args)
{
Salary s = new Salary ("Mohd Mohtashim", "Ambehta, Up", 3, 3600.00);
Employee e = new Salary ("John Adams", "Boston, MA", 2, 2400.00);

  System.out.println("Call mailCheck using Salary reference --");  s.mailCheck();  System.out.println("\n Call mailCheck using Employee reference--");  e.mailCheck();}

}

The results of the above program compilation Run as follows:

Constructing an Employee
Constructing an Employee
Call MailCheck using Salary reference--
Within MailCheck of Salary class
Mailing Check to Mohd Mohtashim with salary 3600.0

Call MailCheck using Employee reference--
Within MailCheck of Salary class
Mailing Check to John Adams with salary 2400.

Abstract methods

If you want to design a class that contains a special member method whose implementation is determined by its subclasses, you can declare the method as an abstract method in the parent class.

The abstract keyword can also be used to declare an abstraction method, which contains only one method name and no method body.

Abstract methods are not defined, followed by a semicolon, not a curly brace, directly following the method name.
Public abstract class Employee
{
private String name;
Private String address;
private int number;

public abstract double Computepay ();

The rest of the code
}

Declaring an abstract method can result in the following two results:

如果一个类包含抽象方法,那么该类必须是抽象类。任何子类必须重写父类的抽象方法,或者声明自身为抽象类。

Subclasses of inherited abstract methods must override this method. Otherwise, the subclass must also be declared as an abstract class. Eventually, a subclass must implement the abstract method, otherwise, from the original parent class to the final subclass, it cannot be used to instantiate the object.

If the salary class inherits the employee class, it must implement the Computepay () method:
Salary.java File Code:
/* File name: Salary.java */
public class Salary extends Employee
{
private double salary; Annual salary

Public double Computepay ()
{
System.out.println ("Computing salary pay for" + getName ());
return SALARY/52;
}

The rest of the code
}
Abstract class Summary provisions

1. 抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过。只有抽象类的非抽象子类可以创建对象。2. 抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类。3. 抽象类中的抽象方法只是声明,不包含方法体,就是不给出方法的具体实现也就是方法的具体功能。4. 构造方法,类方法(用static修饰的方法)不能声明为抽象方法。5. 抽象类的子类必须给出抽象类中的抽象方法的具体实现,除非该子类也是抽象类。

Abstract classes in Ava cannot be instantiated
code example:

Class abstract A {

}

Drive function:

public static void Main (string[] args) {

The following line causes the compilation to not pass
A a=new a ();

}

The child abstract class inherits the parent abstract class and must explicitly call the dominant constructor of the parent abstract class

Abstract class animal{
private int age;
Public Animal (int.) {
This.age = age;
SYSTEM.OUT.PRINTLN ("Initialize Animal");
}
public void Move () {
System.out.println ("Number of Runs:" +this.age);
}
}

Abstract class Dog extends animal{
Public Dog (int.) {
Super (age);//get Rid of the report exception
SYSTEM.OUT.PRINTLN ("Initialize Dog");
}

}

public class BigDog extends dog{
Public BigDog () {
Super (10);
SYSTEM.OUT.PRINTLN ("Initialize BigDog");
}

public static void Main (string[] args) {
BigDog a = new BigDog ();
A.move ();
}
}

Java abstract class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.