"Java4android" Video learning notes-abstract classes and abstract functions

Source: Internet
Author: User

The grammatical characteristics of abstract functions

1, the class is abstract, the object is concrete. There is a very important feature in object-oriented: abstract first, then concrete.

2, only the function definition, and no function body function is called abstract function.

abstract void fun ();

(Definition of function: Return value type, function name, parameter list the definition of the function.) )

3 . Syntax: If you have one or more functions in a class that are abstract, then this class must also be declared as an abstract class.

Class Person

{
Person () {
System.out.println ("constructor of person");
}
String name;
int age;


void introduce () {
System.out.println ("My name is" + name + ", my age is" + ");
}


abstract void eat ();
}

This class compilation error was found after compiling because the class has an abstract void eat (), which, according to the syntax above, compiles only if the person class is also an abstract class.

abstract class person
{
Person () {
System.out.println ("constructor of person");
}
String name;
int age;


void introduce () {
System.out.println ("My name is" + name + ", my age is" + ");
}


abstract void eat ();
}

4 . Syntax: Abstract classes cannot generate objects.

Class Test
{
public static void Main (String args[]) {
person person = new person ();


Person.introduce ();
}
}

Compilation found compilation error. It is not possible to generate objects because the person is an abstract class. Let's take a closer look at why abstract classes cannot generate objects.

Assuming that an abstract class person can generate an object, it means that it is possible to invoke the abstract void eat () of the person, but this function has no method body. The problem cannot be explained. Therefore, an abstract class cannot generate an object.

5, abstract class is born to be father, is used to do the parent class. But the problem is that abstract classes cannot generate objects, and its subclasses inherit it, stating that its subclasses cannot generate objects.

In fact, we have another option: replication. Recalling the definition of replication: Replication (@Override), in two of classes with a parent-child relationship, in these two classes, two functions are defined exactly the same, the relationship between the two functions is called a replication.

Class Chinese extends person
{
void Eat () {
System.out.println ("Eat with Chopsticks");
}
}

At this point, Chinese does not become an abstract class, and it inherits the parent class person. Because the abstract function eat () is replicated, there is no abstract function in the Chinese class at this time.

Since Chinese is not an abstract class, it is possible to generate objects, and then we can call the functions in the parent class by means of an upward transformation.

Class Test
{
public static void Main (String args[]) {
Person person = new Chinese ();

Person.eat ();
}
}

6, syntax: If there is no abstract function in a class, then this class can also be declared as an abstract class.

How do you understand this sentence?

If we define a class, this class we do not want others to build its object, even if there is no abstract function in this class, when the people who have the has evil intentions to use our class, he can not generate objects.

Can abstract classes have constructors?

First we have to understand two points:

1) Abstract classes cannot generate objects;

2) constructors are used to generate class objects;

At these two points, there seems to be no reason for abstract classes to have constructors. Haha, this is because you think less of a problem, you neglect the role of suoer. Here's a proof that an abstract class can have constructors:

Abstract class Person
{
Person () {
System.out.println ("constructor of person");
}
String name;
int age;


void introduce () {
System.out.println ("My name is" + name + ", my age is" + ");
}


abstract void eat ();
}

Class Chinese extends person
{

Super ();


Chinese () {
System.out.println ("Chinese constructor");
}
void Eat () {
System.out.println ("Eat with Chopsticks");
}
}

Class Test
{
public static void Main (String args[]) {
Person person = new Chinese ();


Person.eat ();
}
}

Display Result: Person's constructor

The Chinese constructor

Eat with chopsticks

Before learning super, I said that when you make a subclass object, you must first call the parent class's constructor, and if you don't call the parent class's constructor, the computer will automatically add super () to you, and this line of code calls the constructor of the parent class.

The above example proves that an abstract class can have a constructor, which is prepared for the invocation of a subclass.

"Java4android" Video learning notes-abstract classes and abstract functions

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.