Simple learning Java abstract class essentials and examples _java

Source: Internet
Author: User

Some important points to be aware of using abstract classes:

A class that contains one or more abstract methods must be declared as an abstract class.
Declaring a class as an abstract class does not necessarily contain an abstract method.
It is generally assumed that specific methods should not be included in abstract classes, and it is advisable to place common fields and methods in the superclass as much as possible.
An abstract class cannot be instantiated. That is, you cannot create an object of this class
Instance code:

Copy Code code as follows:

Import java.util.*;

/**
* This program demonstrates abstract classes.
* @version 1.01 2004-02-21
* @author Cay Horstmann
*/
public class Persontest
{
public static void Main (string[] args)
{
Person[] people = new person[2];

Fill the people array with Student and Employee objects
People[0] = new Employee ("Harry Hacker", 50000, 1989, 10, 1);
PEOPLE[1] = new Student ("Maria Morris", "Computer Science");

Print out names and descriptions to all objects
for (person P:people)
System.out.println (P.getname () + "," + p.getdescription ());
}
}

Abstract class Person
{
Public person (String N)
{
name = N;
}

Public abstract String getdescription ();

Public String GetName ()
{
return name;
}

private String name;
}

Class Employee extends Person
{
Public Employee (String N, double S, int year, int month, int day)
{
Super (n);
Salary = s;
GregorianCalendar calendar = new GregorianCalendar (year, month-1, day);
Hireday = Calendar.gettime ();
}

Public double getsalary ()
{
return salary;
}

Public Date Gethireday ()
{
return hireday;
}

Public String getdescription ()
{
Return String.Format ("an employee with a salary of $%.2f", salary);
}

public void Raisesalary (double bypercent)
{
Double raise = salary * BYPERCENT/100;
Salary + = raise;
}

private double salary;
Private Date Hireday;
}

Class Student extends Person
{
/**
* @param n The student ' s name
* @param m The student ' s major
*/
Public Student (string n, String m)
{
Pass N to Superclass constructor
Super (n);
Major = M;
}

Public String getdescription ()
{
Return "A student majoring in" + major;
}

Private String major;
}

In code block:

Copy Code code as follows:

for (person P:people)
System.out.println (P.getname () + "," + p.getdescription ());

P.getdescription (), a method that references the subclass object of a specific subclass.

GetDescription () in the person class cannot be omitted because the compiler allows only the methods declared in the class to be invoked.

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.