Java-- abstract and interfaces

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45613931/article/details/101858891


I was a novice, to write blog for self-review, self-summary.
If wrong, please point out heavyweights.
Reference materials: zero-based learning Java


abstract

Abstract thought is the idea of establishing a program.
The object is to have a common abstract methods and properties extracted, that is common to all objects of extraction,
after extraction, re-design a more versatile, more popular class, called an abstract class.

Abstract refers to the class having common characteristics, which is represented by the keyword abstract.
These abstract classes only define the methods and properties of common features, but there is no specific method to achieve common characteristics.
Having one or more abstract method of the abstract class itself must be defined as an abstract class.

Abstract method is called with the keyword abstract representation. An abstract class can have only abstract methods, can also have a specific method, a class as long as there is an abstract method, then this class is an abstract class.

An abstract class can be inherited, if not all subclasses of abstract methods abstract class, the subclass is also abstract class. If all of the abstract methods abstract class, then subclass is not abstract. Abstract class contains methods must be an abstract class, an abstract class, but do not necessarily contain abstract methods, may be a method are all specific.

Abstract classes are not be instantiated, i.e., can not be used to generate a new instance of the object, but can declare a variable to point to the object abstract class concrete subclasses.

The benefits of an abstract class that when some do not want to implement the method in the parent class, you can not achieve.
Another use of abstract classes are: defining one in an abstract class, generic abstract method that allows subclasses to achieve, so that we can ensure the consistency of the whole program.

Example:

public class Abstract {
	public static void main(String args[])//主方法
	{
		student1 s1=new student1();
		student3 s3=new student3();
		s1.setschoolname("理工");
		s1.setclassname("计算机");
		s1.setlocation("长春");
		s1.setstudentname("李");
		s1.setstudentcode("113");
		s1.setstudentsexy("男");
		s1.setstudentbirthday("1998");
		s3.setschoolname("四川大学");
		s3.setclassname("金融");
		s3.setlocation("四川");
		s3.setstudentname("李一");
		s3.setstudentcode("112");
		s3.setstudentsexy("女");
		s3.setstudentbirthday("1999");
		System.out.println(s1.tostring());
		System.out.println(s3.tostring());
	}
}
//创建一个抽象类 
abstract class school2{
	String schoolname;
	String classname;
	String location;
	//通过设置器来设置各个参数
	public void setschoolname(String schoolname) {
		 this.schoolname=schoolname;	
	}
	public void setclassname(String classname) {
		this.classname=classname;	
	}
    //通过访问器来获得对象的参数
	public String getschoolname() {
		return schoolname;
	}	
	public String getclassname() {
		return classname;
	}
	//设计抽象方法
	abstract void setlocation(String location);
	abstract String getlocation();
	//重写tostring()方法
	public String tostring()
	{
		String infor="学校名称:"+schoolname+" 班级名称:"+classname;
		return infor;
	}
}
//创建学生类,继承抽象类school2
class student1 extends school2{
	//创建成员变量
	String studentname;
	String studentcode;
	String studentsexy;
	String studentbirthday;
	void setlocation(String location){
		this.location=location;
	}
	String getlocation(){
		return location;
	}
	void setstudentname(String studentname) {
		this.studentname=studentname;			
	}
	void setstudentcode(String studentcode) {
		this.studentcode=studentcode;	
	}
	void setstudentsexy(String studentsexy) {
		this.studentsexy=studentsexy;
	}
	void setstudentbirthday(String studentbirthday) {
		this.studentbirthday=studentbirthday;
	}
	String getstudentname() {
		return studentname;
	}
	String getstudentcode() {
		return studentcode;
	}
	String getstudentsexy() {
		return studentsexy;
	}
	String getstudentbirthday() {
		return studentbirthday;
	}
	//通过tostring()方法来让对象以字符串的形式输出
	public String tostring(){
		String infor=super.tostring()+" 学校地址:"+location+" 学生姓名:"+
                studentname+" 学号:"+studentcode+" 性别:"+studentsexy+
                " 出生年月:"+studentbirthday;
		return infor;
	}
}
//设计抽象类student2,继承抽象类school2
abstract class student2 extends school2{
	//创建成员变量
	String studentname;
	String studentcode;
	String studentsexy;
	String studentbirthday;
	//设计抽象方法
	abstract void setlocation(String location);
	abstract String getlocation();
	void setstudentname(String studentname) {
		this.studentname=studentname;			
	}
	void setstudentcode(String studentcode) {
		this.studentcode=studentcode;	
	}
	void setstudentsexy(String studentsexy) {
		this.studentsexy=studentsexy;
	}
	void setstudentbirthday(String studentbirthday) {
		this.studentbirthday=studentbirthday;
	}
	String getstudentname() {
		return studentname;
	}
	String getstudentcode() {
		return studentcode;
	}
	String getstudentsexy() {
		return studentsexy;
	}
	String getstudentbirthday() {
		return studentbirthday;
	}
	//通过tostring()方法来让对象以字符串的形式输出
	public String tostring(){
		String infor=super.tostring()+" 学生姓名:"+
                studentname+" 学号:"+studentcode+" 性别:"+studentsexy+
                " 出生年月:"+studentbirthday;
		return infor;
	}
}
//设计了类student3,继承类student2
class student3 extends student2{
	void setlocation(String location){
		this.location=location;
	}
	String getlocation(){
		return location;
	}
	//通过tostring()方法来让对象以字符串的形式输出
	public String tostring(){
		String infor=super.tostring()+" 学校地址:"+location+";";
		return infor;
	}
}

interface

Only shows the class should do, but does not specify what should be done. That is, only the method name, there is no method body.

Implement the interface: let the name of both methods have method body.

Use interface: In this block, the interface as the norm, when you want to create a student class, student class will use this interface to implement school. It is to make the entire block of the same type of class has a unified standard, so to see the definition of the interface, we know to achieve its class functionality.

Note:
1) Statement class needs to implement the specified interface.
2) providing a defined interface in all methods.
3) the interface is the interface, class is class, interface ≠ class.
4) interfaces to complement inherited deficiencies. Because inheritance must be single inheritance, and interfaces, a class can implement multiple interfaces.
5) To generate an interface instance: SC = School new new Student ();
. 6) in the interface, and instance fields can not be declared static but can be declared constants.
7) the interface can also be inherited.
8) From now, the interface is not just a specifications, is a programming ideas. All methods and properties of the interface, represents the basic idea behind the class to be designed, these methods represent the needs of the program.

Example:

//声明一个学校接口,来告诉程序需要做些什么
	interface school   //接口声明默认为public
	{
		//接口中包括了很多方法,但是都没有实现
		void setschoolname(String schoolname);
		void setclassname(String schoolclassname);
		void setname(String name);
		void setcode(String code);
		void setsexy(String sexy);
		void setbirthday(String birthday);
		String getschoolname();
		String getclassname();
		String getname();
		String getcode();
		String getsexy();
		String getbirthday();
	}
	interface school1 extends school //接口也可以继承
	{
		void setname(String name);
		void setcode(String code);
		void setsexy(String sexy);
		void setbirthday(String birthday);
		String getname();
		String getcode();
		String getsexy();
		String getbirthday();
	}
	//创建一个类,让它实现学校这个接口
	class student implements school1  //关键字implements
	{
		private String schoolname;
		private String classname;
		private String studentname;
		private String studentcode;
		private String studentsexy;
		private String studentbirthday;

//通过设置器来设置各个参数
		
		public void setschoolname(String schoolname) {
			 this.schoolname=schoolname;	
		}

		
		public void setclassname(String classname) {
			this.classname=classname;	
		}

		
		public void setname(String studentname) {
			this.studentname=studentname;			
		}

		
		public void setcode(String studentcode) {
			this.studentcode=studentcode;	
		}

		
		public void setsexy(String studentsexy) {
			this.studentsexy=studentsexy;
		}

		
		public void setbirthday(String studentbirthday) {
			this.studentbirthday=studentbirthday;
		}

	    //通过访问器来获得对象的参数
		public String getschoolname() {
			return schoolname;
		}

		
		public String getclassname() {
			return classname;
		}

		
		public String getname() {
			return studentname;
		}

		
		public String getcode() {
			return studentcode;
		}

		
		public String getsexy() {
			return studentsexy;
		}

		
		public String getbirthday() {
			return studentbirthday;
		}
		//通过tostring方法来让对象以字符串形式输出
		public String tostring()
		{
			String infor="学校名称:"+schoolname+" 班级名称:"+classname+" 学生姓名:"+
		                 studentname+" 学号:"+studentcode+" 性别:"+studentsexy+
		                 " 出生年月:"+studentbirthday;
			return infor;
		}
	}
	//主运行函数
public class Interface {	
	public static void main(String args[])  //主方法
	{
		student st1=new student();
		student st2=new student();
		st1.setschoolname("理工大学");
		st1.setclassname("计算机一班");
		st1.setname("李");
		st1.setcode("11331");
		st1.setsexy("男");
		st1.setbirthday("1998-07-21");
		st2.setschoolname("理工大学");
		st2.setclassname("计算机二班");
		st2.setname("孙");
		st2.setcode("11332");
		st2.setsexy("女");
		st2.setbirthday("1998-10-09");
		System.out.println(st1.tostring());
		System.out.println(st2.tostring());
	 }
}

Abstract interface in common:

1) does not create an object instance, because they are abstract;
2) Although not create object instances with the new new, but can declare variables, the variable pointing object implementation class or subclass, to create an object instance. (The transformation of the object)

difference:

1) Java does not support multiple inheritance, but a subclass can implement multiple interfaces.
2) the instance fields can not have an interface, with only a static variable. An abstract class can have instance fields.
3) the method for automatically setting the interface to the public, abstract class method declaration must manually access specifier.

(Abstract interface, with most of the interface)

Guess you like

Origin blog.csdn.net/qq_45613931/article/details/101858891