Java detailed abstract classes and interfaces difference

What is an abstract class

In a "category", if there is an abstract method, then this class is called an abstract class, and needs before abstract class.
Abstract methods: one method if no method thereof, the method called abstract method, because in multi-state, the process is often redundant parent body, the selection method of forming an abstract method discarded. Its code form:


public abstract class Mammal {//父类
	
	public abstract void move();
}

Detailed abstract class

1, if the parent class abstract method, you must subclass so abstract methods, otherwise the subclass must be abstract class, abstract class and can not create object.

public abstract class Mammal {//父类
	
	public abstract void move();
	
	public abstract void breathe();
}

Subclass can be written as:

public class Whale extends Mammal{//子类

	@Override
	public void move() {
		System.out.println("靠鱼鳍游泳");
	}
	
	@Override
	public void breathe() {
		System.out.println("靠鱼鳃游泳");
	}
}

or:

public abstract class Whale extends Mammal{//子类

}

2, abstract class can not create an object, but can have constructor.

public abstract class Mammal {//父类
	
	public Mammal(int age) {//可以创建Mammal方法
		//Mammal mammal = new Mammal();mammal不能创建对象
	}
	
	public abstract void move();
	
	public abstract void breathe();
}

3, an abstract class is not an abstract method can, but must abstract method is an abstract class
4, the abstract class does not (directly reflected polymorphism) by the final modification.
5, the difference between the normal class abstract class:
. A class can not create abstract object class may be common;
B have the abstract class keyword abstract modification, there is no general category;.
C abstract class can have abstract methods, the general category. certainly no abstract methods.

What is the interface

If all the methods in a source file, the entire abstract method, then this may be defined for the interface source file. Code form as follows:

public interface IMammal {

		public abstract void move();
		
		public abstract void breathe();

}

Detail Interface

1, there is no interface constructor.
2, interface method defaults to public abstract, it can be ignored.

public interface IMammal {

	/*public IMammal() {//不能构造方法
		
	}*/
	
	void move();//忽略public abstract
		
	void breathe();//忽略public abstract

}

3, an interface can inherit multiple interfaces.

interface IA{//接口A
	
}

interface IB{//接口B
	
}
public interface Mammal extends IA,IB{//继承接口A,B
	
	void move();
		
	void breathe();

}

4, states: interface name begins with I (code specifications)
5, the interface variables default to public static final (ie are static constants). So named variables in the interface should be all caps.

public interface IMammal{
	
	void move();
		
	void breathe();

	int AGE = 957;
}

public class Test {

	public static void main(String[] args) {
		//IMmammal.AGE = 125;  //不可实现,代码报错,从而证明5。
		System.out.println(IMammal.AGE);
		
	}
}

6, the interface inherited abstract methods, require the use of implements.
7, when implementing interface, you can inherit the constants defined in the interface.

public interface IMammal{
	
	void move();
		
	void breathe();

	int AGE = 957;
}

public class Whale implements IMammal{

	@Override
	public void move() {
		
	}

	@Override
	public void breathe() {
		
	}

}
public class Test {

	public static void main(String[] args) {
		
		System.out.println(IMammal.AGE);
		System.out.println(Whale.AGE);
		
	}
}

8, a class if all the abstract methods do not implement the interface, then the class must be abstract.

public interface IMammal{
	
	void move();
		
	void breathe();

	int AGE = 957;
}

public abstract class IWhale implements IMammal{
	@Override
	public void move() {
		
	}
}

9, a class can implement multiple interfaces.

public abstract class IWhale implements IMammal,List{

	@Override
	public void move() {
		
	}

	@Override
	public void breathe() {
		
	}
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

}

10, instanceof to determine whether the object pointed to the front of the latter type, the result is a boolean type.

public class Test {

	public static void main(String[] args) {
		
		Whale whale = new Whale();
		
		System.out.println(whale instanceof Whale);
		System.out.println(whale instanceof Object);//Object为所有类的父类
		System.out.println(whale instanceof IMmammal);
		
		
	}
}

The result is:
to true
to true
to true

the difference

1, and the object constructor:
abstract class can not create an object, but can have constructor;
the interface interface can not directly use the new keyword to create an object, but is not limited and it can not create an object, the method can not be abstract.
2, the modifier:
abstract class need to add abstract before class;
interfaces need to add interface before the interface name, and no class.

Published 19 original articles · won praise 0 · Views 456

Guess you like

Origin blog.csdn.net/zzu957/article/details/105014395