java抽象类_Java抽象类

java抽象类

Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also.

Java中的抽象类与接口类似,不同之处在于它可以包含默认方法实现。 抽象类可以具有不带主体的抽象方法,也可以具有带实现的方法。

abstract keyword is used to create a abstract class and method. Abstract class in java can’t be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.

abstract关键字用于创建抽象类和方法。 Java中的抽象类无法实例化。 抽象类通常用于为子类提供基础,以扩展和实现抽象方法,并覆盖或使用抽象类中的已实现方法。

Java抽象类 (Abstract Class in Java)

Here is a simple example of an Abstract Class in Java.

这是Java中的Abstract类的简单示例。

package com.journaldev.design;

//abstract class
public abstract class Person {
	
	private String name;
	private String gender;
	
	public Person(String nm, String gen){
		this.name=nm;
		this.gender=gen;
	}
	
	//abstract method
	public abstract void work();
	
	@Override
	public String toString(){
		return "Name="+this.name+"::Gender="+this.gender;
	}

	public void changeName(String newName) {
		this.name = newName;
	}	
}

Notice that work() is an abstract method and it has no-body. Here is a concrete class example extending an abstract class in java.

请注意, work()是一种抽象方法,它没有主体。 这是一个扩展Java中抽象类的具体类示例。

package com.journaldev.design;

public class Employee extends Person {
	
	private int empId;
	
	public Employee(String nm, String gen, int id) {
		super(nm, gen);
		this.empId=id;
	}

	@Override
	public void work() {
		if(empId == 0){
			System.out.println("Not working");
		}else{
			System.out.println("Working as employee!!");
		}
	}
	
	public static void main(String args[]){
		//coding in terms of abstract classes
		Person student = new Employee("Dove","Female",0);
		Person employee = new Employee("Pankaj","Male",123);
		student.work();
		employee.work();
		//using method implemented in abstract class - inheritance
		employee.changeName("Pankaj Kumar");
		System.out.println(employee.toString());
	}

}

Note that subclass Employee inherits the properties and methods of superclass Person using inheritance in java.

请注意,子类Employee使用java中的继承继承了超类Person的属性和方法。

Also notice the use of Override annotation in Employee class. Read more for why we should always use Override annotation when overriding a method.

还要注意在Employee类中使用Override 注释 。 阅读更多有关为什么我们在覆盖方法时应始终使用Override注释的信息

Java重要点中的抽象类 (Abstract class in Java Important Points)

  1. abstract keyword is used to create an abstract class in java.

    abstract关键字用于在Java中创建抽象类。
  2. Abstract class in java can’t be instantiated.

    Java中的抽象类无法实例化。
  3. We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.

    我们可以使用abstract关键字来创建一个abstract方法,abstract method没有主体。
  4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.

    如果类具有抽象方法,则该类也应该使用abstract关键字进行抽象,否则它将无法编译。
  5. It’s not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods.

    抽象类不必具有抽象方法。 即使没有声明任何抽象方法,我们也可以将其标记为抽象类。
  6. If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.

    如果抽象类没有任何方法实现,则最好使用接口,因为Java不支持多类继承。
  7. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.

    Java中抽象类的子类必须实现所有抽象方法,除非该子类也是抽象类。
  8. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.

    除非接口方法是静态的或默认的,否则接口中的所有方法都是隐式抽象的。 Java 8中添加了接口中的静态方法和默认方法,有关更多详细信息,请阅读Java 8接口更改
  9. Java Abstract class can implement interfaces without even providing the implementation of interface methods.

    Java Abstract类可以实现接口,甚至不提供接口方法的实现。
  10. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.

    Java Abstract类用于为所有子类提供通用方法实现或提供默认实现。
  11. We can run abstract class in java like any other class if it has main() method.

    如果有main()方法,我们可以像其他任何类一样在Java中运行抽象类。

That’s all for an abstract class in Java. If I missed anything important, please let us know through comments.

这就是Java中抽象类的全部内容。 如果我错过任何重要事项,请通过评论告知我们。

翻译自: https://www.journaldev.com/1582/abstract-class-in-java

java抽象类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值