JAVA inheritance (1) class inherits abstract class

[b] Mainly use the realization of the program to indicate that the class inherits the abstract class[/b] It

describes the use of a class to inherit an abstract class and the concrete realization of the abstract methods in the class.

Parent Class:
abstract class definition: public abstract class Student {}
features: abstract class may contain methods which may contain specific method
abstract methods defined: public abstract void infor (String newName , String newSex);   Features: The abstract method has no method body, and specific steps are needed to implement this abstract
method. 
Subclass:
public class UNStudent extends Student{}
used to implement all the methods of the abstract class, you can also have your own methods, or you can override the parent class methods write
public void infor (String newName, String newSex) {}
abstract method implementation process of

the test classes:
class {} StudentTest
examples of the use of the concrete implementation of the abstract method represented
UNStu.infor ( "Nancy", "female");
sub The class calls the public method of the parent class and implements it
UNStu.study();

[b][b] The complete code is as follows:[/b][/b]

//Create a college student class to inherit the abstract class
public class UNStudent extends Student{

public void infor(String newName,String newSex){
name=newName;
sex=newSex;
System.out.println ("I am"+name+"is "+sex+"!!");
}
//Implement the student's name and gender

}

//student test
class StudentTest{
public static void main(String[] args){
UNStudent UNStu=new UNStudent();
UNStu.infor("Nancy","girl");
UNStu.study();
}

}

//Create an abstract student class
public abstract class Student{
String name;
String sex;
//Attribute
public abstract void infor(String newName,String newSex);
//Enter the name and gender of the student
public void study(){
//Specific learning method
System.out.println("study everyday!!");
}
}

Guess you like

Origin blog.csdn.net/u010682774/article/details/84429919