Object-oriented (abstract class exercises) and object-oriented abstract class exercises

Source: Internet
Author: User
Tags class manager define abstract

Object-oriented (abstract class exercises) and object-oriented abstract class exercises

   

/**
* Created by rabbit on 2014-07-21. blogpark. Liu pengcheng
* If we need to model employees when developing a system,
* An employee has three attributes: name, employee ID, and salary. The manager is also an employee
* In addition to employee attributes, there is also a bonus attribute.
* Use inheritance to design employee and manager classes. Requirement class
* Provide a necessary method for Attribute access
*
* Employee class: name, id, pay
*
* Manager class: inherits employees and has their own bonus.
*/
// Created by rabbit on 2014-07-21. Blog garden. Liu pengcheng
Abstract class Employee
{
Private String name;
Private String id;
Private int pay;

   

Employee (String name, String id, int pay)
{
This. name = name;
This. id = id;
This. pay = pay;
}
Public abstract void work ();
}
// Created by rabbit on 2014-07-21. Blog garden. Liu pengcheng
Class Manager extends Employee
{
Private int bonus;
Manager (String name, String id, int pay, int bonus)
{
Super (name, id, pay );
This. bonus = bonus;
}

   

Public void work ()
{
System. out. println ("Manager work ");
}
}
// Created by rabbit on 2014-07-21. Blog garden. Liu pengcheng
Class Pro extends Employee
{
Pro (String name, String id, int pay)
{
Super (name, id, pay );
}
Public void work ()
{
System. out. println ("Pro work ");
}

   

}
// Created by rabbit on 2014-07-21. Blog garden. Liu pengcheng
Public class AbstractTest
{
Public static void main (String [] args)
{
System. out. println ("work ");
}
}


In object-oriented technology, the abstract class is ____

D

An abstract class is a class that only declares methods that do not define (implement). It has two concepts:
Abstract classes must be inherited by subclasses, that is, they must be implemented by classes.
Defining messages is the implementation of methods.

For example: abstract class A {void print ();}
Class B extends A {void print () {System. out. println ("hello ");
}}
In abstract class A, declare A method to implement A method in Class B.

Different interfaces and abstract classes

Abstract class and interface are two mechanisms supported for the definition of abstract classes in Java. It is precisely because of the existence of these two mechanisms that give Java powerful object-oriented capabilities. Abstract class and interface have great similarity in support for the definition of abstract classes, and can even be replaced with each other, therefore, when defining abstract classes, many developers may choose abstract classes and interfaces at will. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable. This article will analyze the differences between them and try to provide developers with a basis for selection between them.

Understanding abstract classes

Abstract class and interface are used for abstract classes in Java language (abstract classes in this article are not translated from abstract class, it represents an abstract body, abstract class is a method used to define abstract classes in the Java language. Please note that it is defined). So what is an abstract class and what benefits can it bring to us by using abstract classes?

In the concept of object-oriented, we know that all objects are depicted through classes, but this is not the case. Not all classes are used to depict objects. If a class does not contain enough information to depict a specific object, such classes are abstract classes. Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains. They are abstractions of a series of seemingly different but essentially identical specific concepts. For example, if we develop a graphic editing software, we will find that some specific concepts such as circles and triangles exist in the problematic domain. They are different, however, they all belong to the concept of shape. The concept of shape does not exist in the field of problem. It is an abstract concept. Abstract concepts cannot be instantiated because they do not have specific concepts in the problem field.

In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions, but this group of actions can have any specific implementation method. This abstract description is an abstract class, and any possible implementations of this group are represented as all possible Derived classes. The module can operate on an abstract body. Because the module depends on a fixed abstract body, it may not be allowed to be modified. At the same time, the behavior function of this module can be extended by deriving from this abstract body. Readers familiar with OCP must know that abstract classes are the key to implementing an Open-Closed Principle of object-oriented design.

Abstract class and interface in terms of syntax definition

At the syntax level, the Java language provides different definitions for abstract class and interface. The following describes how to define an abstract class named Demo.

You can use abstract class to define a Demo abstract class as follows:

Abstract class Demo {
Abstract void method1 ();
Abstract void method2 ();
...
}

The following method is used to define the Demo abstract class using the interface:

Interface Demo {
Void method1 ();
Void method2 ();
...
}

In the abstract class method, the Demo can have its own data members or non-abstarct member methods. In the implementation of the interface method, demo can only have static data members that cannot be modified (that is, they must be static final, but generally do not define data members in the interface). All member methods are abstract. In a sense, inte... the remaining full text>

Related Article

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.