Java中的抽象与公共访问修饰符
Java中的访问修饰符是用于定义类、变量和方法的范围的保留关键字。它还告诉我们是否可以创建子类或是否可以创建对象。
抽象访问修饰符是仅适用于类和方法但不适用于变量的修饰符。如果我们将任何方法声明为抽象,那么该方法必须在相应类的子类中实现,因为抽象方法从不谈论实现。如果任何修饰符谈论实现,那么它与抽象修饰符形成非法组合。以类似的方式,如果对于任何Java类,如果我们不允许创建对象(因为部分实现),那么我们必须使用抽象修饰符声明这种类型的类。
例子:
Java
// Java program to illustrate Abstract Access Modifier
// Importing the required packages
import java.io.*;
import java.util.*;
// Class 1
// Helper abstract class
abstract class Vehicle {
// Declaring an abstract method getNumberOfWheel
abstract public int getNumberOfWheel();
}
// Class 2
// Helper class extending above abstract class
class Bus extends Vehicle {
// Giving the implementation
// to the parent abstract method
public int getNumberOfWheel() { return 7; }
}
// Class 3
// Helper class extending above abstract class
class Auto extends Vehicle {
// Giving the implementation
// to the parent abstract method
public int getNumberOfWheel() { return 3; }
}
// Class 4
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating Bus object
Bus b = new Bus();
// Creating Auto object
Auto a = new Auto();
// Now getting and displaying
// the number of wheels
// for Bus by calling the
// getNumberOfWheel method
System.out.println("Number of wheels in bus is"
+ " " + b.getNumberOfWheel());
// Now getting and displaying
// the number of wheels
// for Auto by calling the
// getNumberOfWheel method
System.out.println("Number of wheels in Auto is"
+ " " + a.getNumberOfWheel());
}
}
Java
// Java Program to illustrate public modifier
// Creating a package pack1
package pack1;
// Declaring a class A
public class A {
// Declaring a public method m1
public void m1()
{
// Print statement
System.out.println("GFG");
}
}
Java
// Java Program to Illustrate Public Modifier
// Creating package pack2
package pack2;
// Importing package pack1;
import java.io.*;
// Importing the required modules
import java.util.*;
import pack1.*;
// Main class
// Declaring a class B
class B {
// Main driver method
public static void main(String[] args)
{
// Creating object of type class A
A b = new A();
// Now calling the method m1()
A.m1();
}
}
Number of wheels in bus is 7
Number of wheels in Auto is 3
现在关注下一个修饰符是公共访问修饰符。所以 如果一个类被声明为 public,那么我们可以从任何地方访问该类。我们将在该包中创建一个包 pack1,我们声明一个公共类 A,在该类中,我们声明一个方法 m1,它也是公共的。现在我们创建另一个包pack2,在这个pack2中我们导入pack1并声明一个类B,在类B的main方法中,我们创建一个类A的对象并尝试访问方法m1的数据。
示例 1:
Java
// Java Program to illustrate public modifier
// Creating a package pack1
package pack1;
// Declaring a class A
public class A {
// Declaring a public method m1
public void m1()
{
// Print statement
System.out.println("GFG");
}
}
输出:
示例 2:
Java
// Java Program to Illustrate Public Modifier
// Creating package pack2
package pack2;
// Importing package pack1;
import java.io.*;
// Importing the required modules
import java.util.*;
import pack1.*;
// Main class
// Declaring a class B
class B {
// Main driver method
public static void main(String[] args)
{
// Creating object of type class A
A b = new A();
// Now calling the method m1()
A.m1();
}
}
输出:
最后在对两者有充分了解后,让我们总结出它们之间的区别,以便更好地理解,如下表所示: Abstract Access Modifier Public Access Modifier This modifier is not applicable for variables. This modifier is applicable for variables. This modifier is not applicable for enum. This modifier is applicable for both outer and inner enum. This modifier is not applicable for constructors. This modifier is applicable for constructors. This modifier is more restricted than the public access modifier. This modifier is less restricted than the abstract access modifier.