📜  在Java中防止方法覆盖的不同方法

📅  最后修改于: 2022-05-13 01:55:10.238000             🧑  作者: Mango

在Java中防止方法覆盖的不同方法

继承是任何面向对象编程 (OOP) 语言的基本规则,但仍有一些方法可以防止子类中的方法覆盖,如下所示:

方法:

  1. 使用静态方法
  2. 使用私有访问修饰符
  3. 使用默认访问修饰符
  4. 使用final关键字方法

方法一:使用静态方法

这是防止子类中方法覆盖的第一种方法。如果您将任何方法设为静态,那么它就成为类方法而不是对象方法,因此不允许被覆盖,因为它们在编译时解析,而被覆盖的方法在运行时解析。

Java
// Java Program to Prevent Method Overriding
// using a static method
 
// Importing java input output classes
import java.io.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object og Base class
        Base base = new Child();
 
        // Printing message from base class as
        // its static methods have static binding
 
        // Hence even if the object is of Child class
        //  message printed is from base class
        base.hello();
    }
}
 
// Class 2
// Parent class
class Base {
 
    // hello() method of parent class
    public static void hello()
    {
 
        // Print and display the message if
        // hello() method of parent class is called
        System.out.println("Hello from base class");
    }
}
 
// Class 3
// Child class
class Child extends Base {
 
    // Overriding the existing method - hello()
    // @Override
    // hello() method of child class
    public static void hello()
    {
        // Print and display the message if
        // hello() method of child class is called
        System.out.println("Hello from child class");
    }
}


Java
// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of Child class
        Child child = new Child();
 
        // Calling hello() method in main()
        child.hello();
    }
}
 
// Class 2
// Helper class 1
// Child Class
class Child extends Base {
 
    //@Override
    // hello() method of child class
    public void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Helper class 2
// Parent Class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}


Java
// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of base class
        Base base = new Child();
 
        // Calling hello() method using Base class object
        base.hello();
    }
}
 
// Class 2
// Parent class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}
 
// Class 3
// Child class
class Child extends Base {
 
    // Overriding existing method
    // @Override
 
    // Hello method of child class
    void hello()
    {
 
        // Print statement when hello() method
        // of child class is called
        System.out.println("Hello from child class");
    }
}


Java
// Java Program to Prevent Method Overriding
// using a final keyword method
 
// Importing input output classes
import java.io.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Child class
        Child child = new Child();
 
        // Calling hello() method using Child class object
        child.hello();
    }
}
 
// Class 2
// Child class
class Child extends Base {
 
    // Overriding
    // @Override
 
    // Method of child class
    public void hello()
    {
        // Print statement for Child class
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Base class
class Base {
 
    // Method of parent class
    public final void hello()
    {
        // Print statement for Base(parent) class
        System.out.println("Hello from base class");
    }
}


输出
Hello from base class

方法 2使用私有访问修饰符将任何方法设为私有只会将该方法的范围缩小到类,这意味着类之外的任何人都不能引用该方法。

例子

Java

// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of Child class
        Child child = new Child();
 
        // Calling hello() method in main()
        child.hello();
    }
}
 
// Class 2
// Helper class 1
// Child Class
class Child extends Base {
 
    //@Override
    // hello() method of child class
    public void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Helper class 2
// Parent Class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}
输出
Hello from child class

方法 3使用默认访问修饰符

这只能在允许在同一包内但不允许在包外进行方法覆盖时使用。默认修饰符允许该方法仅在包内可见,因此同一包外的任何子类都不能覆盖它。

例子

Java

// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of base class
        Base base = new Child();
 
        // Calling hello() method using Base class object
        base.hello();
    }
}
 
// Class 2
// Parent class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}
 
// Class 3
// Child class
class Child extends Base {
 
    // Overriding existing method
    // @Override
 
    // Hello method of child class
    void hello()
    {
 
        // Print statement when hello() method
        // of child class is called
        System.out.println("Hello from child class");
    }
}

输出:

 
方法四:使用final关键字方法

防止覆盖的最后一种方法是 使用最终 方法中的关键字。 final 关键字停止成为继承。因此,如果一个方法是最终的,它将被视为最终实现,并且没有其他类可以覆盖该行为。

Java

// Java Program to Prevent Method Overriding
// using a final keyword method
 
// Importing input output classes
import java.io.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Child class
        Child child = new Child();
 
        // Calling hello() method using Child class object
        child.hello();
    }
}
 
// Class 2
// Child class
class Child extends Base {
 
    // Overriding
    // @Override
 
    // Method of child class
    public void hello()
    {
        // Print statement for Child class
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Base class
class Base {
 
    // Method of parent class
    public final void hello()
    {
        // Print statement for Base(parent) class
        System.out.println("Hello from base class");
    }
}

输出: