Java Abstract class Learning

Source: Internet
Author: User
Tags class manager

Help documentation <=> API documentation <=> program documentation

Name, age, work number
Employee class
General Employee Class
Manager class

Abstract: Abstraction, which is not specific, can only modify classes and methods, cannot modify variables, and so on.

Abstract method: The method that is modified by the abstract keyword, only the function declaration, no function body.

Abstract class: A class that is abstarct decorated.
Formally, it is the class containing the abstract method, of course, it can also be an abstract method, can also be modified into abstract class, which is equivalent to prohibit
This class creates a class object.
In the sense, abstract class refers to the abstract class described by the things in the analysis, found that a part of the function of the functional content is uncertain, but the function must
Exist. Therefore, this part of the function is defined as an abstract method, there is no function body, only the function declaration.
Note: Abstract classes cannot create objects with new. However, you can declare a reference variable for the abstract class.


After the abstract class covers the class, if the subclass wants to use the functionality in the abstract class, then all abstract methods must be overridden (overwritten), and only all abstract methods are overwritten
(overwrite) to be able to create a subclass object to invoke the method in which the subclass is also an abstract class if it only covers (overrides) part of the abstract method
(That is, the subclass must be defined as an abstract class, because the subclass inherits the parent class, and there is no overriding (overwriting) all of the extraction methods, which is equivalent to the existence of some
Abstract method, so the subclass must be defined as an abstract class, if the subclass is not defined as an abstract class, and no overriding (overwriting) all abstract methods in the parent class
, the compilation will error.

Abstract class and general class there is nothing too big difference, only two points different
1, there are some methods in the abstract class that have no function body, only function declaration.
2, an abstract class cannot create an instance, because it does not make sense to reference a method without a function body after the class object is created.

There is no difference between abstract classes and other parent classes that are extracted from subclasses, except that function definitions in multiple subclasses are the same and feature content is different, so there is no in the parent class
An abstract function arises when there is a need for a method body. Abstract classes cannot create objects, but they can have constructor methods and member variables, member functions, building blocks of code, static
code block. It's no different from the normal parent class, but it can also be accessed using polymorphic techniques or using the Super keyword. There is also an overlay (overwrite). can also be
To directly take over, inherit the parent class (abstract class) method, directly call can.

An abstract method in an abstract class, the method that is modified by an abstraction, is equivalent to forcing a subclass to overwrite (override) The method, or it cannot create the child class object.
The final modification method is just the opposite, forcing a block override (overwrite) of a method.
*/

Abstract class Employee
{
private String name;
private int age;
Private String ID;
Employee (String name,int age,string ID)
{
THIS.name = name;
This.age = age;
This.id = ID;
}
Public String GetName ()
{
return name;
}
public abstract void work ();
}

Class Manager extends Employee
{
private int bonus;
Manager (String name, int age,string id,int bonus)
{
Super (NAME,AGE,ID);
This.bonus = bonus;
}
public void work ()
{
System.out.println ("Manager work");
}
}
Class Pro extends Employee
{
Pro (String name,int age,string ID)
{
Super (NAME,AGE,ID);
}
public void work ()
{
System.out.println ("Pro work");
}
}
Abstract class Ceo extends Employee
{
Ceo (string name, int age, string id)
{
Super (NAME,AGE,ID);
}
public void work ()
{
System.out.println ("Ceo work");
}
}

Java Abstract class Learning

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.