Java Foundation 9 -- Abstract Classes

Keywords: Java

Java Foundation 9 -- Abstract Classes

Introduction of abstract classes


/*
Abstract classes:
Abstraction: General, vague, unable to understand! Not specific.

 

Characteristic:
1. When a method is declared to be unrealized, it is an abstract method and needs to be abstractly modified.
Abstract methods must be defined in abstract classes. This class must also be abstracted.
2. An abstract class cannot be instantiated. Why? Because it makes no sense to call Abstract methods.
3. An abstract class must have its subclasses that cover all abstract methods before it can be instantiated.
Otherwise, this subclass is an abstract class.


1. Do constructors exist in abstract classes?
Yes, for initializing subclass objects.



2. Can abstract classes not define abstract methods?
Tolerable. But rarely, the goal is not to let this class create objects. AWT adapter objects are such classes.
Usually the methods in this class have method bodies, but there is no content.

abstract class Demo
{
void show1()
{}

void show2()
{}
}

 

3. Can Abstract keywords coexist with those keywords?
private doesn't work
Static: No, if it's static, it doesn't need objects, and abstract classes need objects.
final

 

4. The similarities and differences between abstract class and general class.
Similarities:
Both abstract and general classes are used to describe things, and members are defined internally.
Different:
1. General classes have enough information to describe things.
Abstract classes may not have enough information to describe things.
2. In general classes, abstract methods cannot be defined, only non-abstract methods can be defined.
Abstract methods can be defined in abstract classes, and non-abstract methods can also be defined.
3. General classes can be instantiated.
Abstract classes cannot be instantiated.

 


5. Does an abstract class have to be a parent?
Yes. Because subclasses need to override their methods before subclasses can be instantiated.

 


*/

abstract class Demo
{
abstract /* abstract * / void show();

}

/*

class DemoA extends Demo
{
void show()
{
System.out.println("demoa show");
}
}
class DemoB extends Demo
{
void show()
{
System.out.println("demob show");
}
}
*/
abstract class canine family
{
abstract void roar ();
}

class dog extends canine family
{

void roar ()
{
System.out.println("Wang Wang");
}
}
class wolf extends canine family
{
void roar ()
{
System.out.println("Ouch");
}
}

 

class AbstractDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

 

2. Instances of abstract classes

  1 /*
  2 Employee examples:
  3 Requirements: Programmers in the company have names, job numbers, salaries, and job content.
  4 In addition to name, number, salary, bonus and job content, the project manager also has his own name, job number and salary.
  5 Data modeling for given requirements.
  6 
  7 
  8 
  9 Analysis:
 10 In this problem area, first identify the object involved.
 11 Through NOUN extraction.
 12 Programmer:
 13     Attributes: name, number, salary,
 14     Behavior: Work.
 15 Manager:
 16     Attributes: name, number, salary, bonus.
 17     Behavior: Work.
 18 
 19 
 20 There is no direct inheritance relationship between programmers and managers.
 21 
 22 But programmers and managers have something in common.
 23 It can be extracted. Because they are all employees of the company. 
 24 
 25 Programmers and managers can be extracted and systems established.
 26     
 27 */
 28 
 29 //Describe employees.
 30 
 31 abstract class Employee
 32 {
 33     private String name;
 34     private String id;
 35     private double pay;
 36     Employee(String name,String id,double pay)
 37     {
 38         this.name = name;
 39         this.id = id;
 40         this.pay = pay;
 41     }
 42 
 43     public abstract void work();
 44     
 45 }
 46 
 47 
 48 //Describe the programmer.
 49 class Programmer extends Employee
 50 {
 51     Programmer(String name,String id,double pay)
 52     {
 53         super(name,id,pay);
 54     }
 55     public void work()
 56     {
 57         System.out.println("code...");
 58     }
 59 }
 60 
 61 //Describe the manager. 
 62 class Manager extends Employee
 63 {
 64     private int bonus;
 65     Manager(String name,String id,double pay,int bonus)
 66     {
 67         super(name,id,pay);
 68         this.bonus = bonus;
 69     }
 70     public void work()
 71     {
 72         System.out.println("manage");
 73     }
 74 }
 75 
 76 
 77 
 78 
 79 
 80 
 81 class  AbstractTest
 82 {
 83     public static void main(String[] args) 
 84     {
 85         System.out.println("Hello World!");
 86     }
 87 }
 88 
 89 
 90 class Person
 91 {
 92     private String name;
 93     private int age;
 94 
 95     Person(String name,int age)
 96     {
 97         this.name = name;
 98         this.age = age;
 99     }
100     public String getName()
101     {
102         return name;
103     }
104     public void setName(String name)
105     {
106         this.name = name;
107     }
108 }
109 class Student extends Person
110 {
111     Student(String name,int age)
112     {
113         super(name,age);
114     }
115 
116 }
117 class Worker extends Person
118 {
119     Worker(String name,int age)
120     {
121         super(name,age);
122     }
123 
124 }

Posted by greeneel on Wed, 26 Jun 2019 12:16:53 -0700