📜  在子类的继承中使用方法覆盖的Java程序

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

在子类的继承中使用方法覆盖的Java程序

Java中的方法覆盖是指子类实现了一个已经存在于超类中的方法。在方法覆盖的帮助下,我们可以实现运行时多态性。当我们覆盖一个方法时,我们必须记住三件事。

  • 子类中的方法必须与超类中的方法同名。
  • 子类中的方法应该具有相同数量的参数。
  • 子类中方法的返回类型(或子类型)应与超类中方法的返回类型相同。

在本文中,我们将讨论如何在子类的继承中使用方法覆盖。

让我们通过下图来理解子类的方法的覆盖。

在上图中,我们以只有一个方法即 show() 的 Grandpa 类为例。 Dad 类继承了 Grandpa 类并覆盖了 show() 方法。之后 Dad 类被 Me 类继承,它覆盖了 Dad 类的 show() 方法。之后,如果我们想访问上述类的 show() 方法,那么我们可以创建该类的对象,因为将调用的方法仅取决于对象的类型,而不取决于引用变量的类型到那个对象。

让我们使用代码来理解上述概念。

Java
// Java Program to show the overriding using inheritance in
// subclasses
  
// Grandpa class is at the top of
// the inheritance hierarchy
class Grandpa {
    public void show()
    {
        System.out.println(
            "Inside show() method of Grandpa class");
    }
}
  
class Dad extends Grandpa {
    
    // Overriding show method of Grandpa class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Dad class");
    }
}
  
// class Me is inheriting Dad class (i.e.
// a subclass of Grandpa class)
class Me extends Dad {
    
    // Overriding show method of Dad class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Me class");
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating instance of Grandpa class
        Grandpa grandpa = new Grandpa();
        
        // Creating instance of Dad class
        Grandpa dad = new Dad();
        
        // Creating instance of Me class
        Grandpa me = new Me();
        
        // as discussed which show() function will get
        // execute depends upon the type of object
  
        // show function of Grandpa class will get execute
        grandpa.show();
        
        // show function of Dad class will get execute
        dad.show();
        
        // show function of Me class will get execute
        me.show();
    }
}


Java
// Java Program to show the overriding using inheritance in
// subclasses when subclass contains methods other than that
// of the parent/ superclass
  
// Grandpa class is at the top of
// the inheritance hierarchy
class Grandpa {
    public void show()
    {
        System.out.println(
            "Inside show() method of Grandpa class");
    }
}
  
class Dad extends Grandpa {
  
    // Overriding show method of Grandpa class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Dad class");
    }
  
    // Creating a display method() for Dad class
    public void display()
    {
        System.out.println(
            "Inside display() method of class B");
    }
}
  
// class Me is inheriting Dad class (i.e.
// a subclass of Grandpa class)
class Me extends Dad {
  
    // Overriding show method of Dad class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Me class");
    }
  
    @Override public void display()
    {
        System.out.println(
            "Inside display() method of class C");
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
  
        // The code below works the same as in
        // previous example
        Grandpa grandpa = new Grandpa();
        Grandpa dad = new Dad();
        Grandpa me = new Me();
  
        grandpa.show();
        dad.show();
        me.show();
  
        // Making Dad type point to the
        // Dad object
        Dad dad1 = new Dad();
  
        // Making Dad type reference variable point to the
        // Me object
        Dad me1 = new Me();
  
        dad1.display();
        me1.display();
    }
}


输出
Inside show() method of Grandpa class
Inside show() method of Dad class
Inside show() method of Me class

如果子类包含父类/超类以外的方法,会发生什么?

让我们借助下面提到的图来理解这一点。

在上图中,我们在 Dad 类中又采用了一个名为 display() 的方法,该方法在 Me 类中被覆盖。现在,如果我们尝试通过分别创建 Dad 和 Me 类的对象以及 Grandpa 类的引用变量来访问 display() 方法,则会报错,因为该类的引用变量只能用于访问子类的方法已经从层次结构向下覆盖的,即我们不能在调用显示函数时在这里使用爷爷类的引用变量,因为爷爷类在层次结构中最顶层,它不包含显示函数。

如果我们尝试使用 Grandpa() 类的引用变量调用 display()函数,我们将得到以下错误。

dad.display();
me.display();

GFG.java:52: error: cannot find symbol
        dad.display();
           ^
  symbol:   method display()
  location: variable dad of type Grandpa
GFG.java:53: error: cannot find symbol
        me.display();
          ^
  symbol:   method display()
  location: variable me of type Grandpa

我们可以通过使用下面提到的语法使我们的 Dad 类型或 Me 类型的引用变量引用 Dad and Me 类的对象来解决这个问题。

Dad dad1 = new Dad();
Dad me1 = new Me();
dad1.display();
me1.display();

现在,让我们借助下面提到的代码来理解这些概念。

Java

// Java Program to show the overriding using inheritance in
// subclasses when subclass contains methods other than that
// of the parent/ superclass
  
// Grandpa class is at the top of
// the inheritance hierarchy
class Grandpa {
    public void show()
    {
        System.out.println(
            "Inside show() method of Grandpa class");
    }
}
  
class Dad extends Grandpa {
  
    // Overriding show method of Grandpa class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Dad class");
    }
  
    // Creating a display method() for Dad class
    public void display()
    {
        System.out.println(
            "Inside display() method of class B");
    }
}
  
// class Me is inheriting Dad class (i.e.
// a subclass of Grandpa class)
class Me extends Dad {
  
    // Overriding show method of Dad class
    @Override public void show()
    {
        System.out.println(
            "Inside show() method of Me class");
    }
  
    @Override public void display()
    {
        System.out.println(
            "Inside display() method of class C");
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
  
        // The code below works the same as in
        // previous example
        Grandpa grandpa = new Grandpa();
        Grandpa dad = new Dad();
        Grandpa me = new Me();
  
        grandpa.show();
        dad.show();
        me.show();
  
        // Making Dad type point to the
        // Dad object
        Dad dad1 = new Dad();
  
        // Making Dad type reference variable point to the
        // Me object
        Dad me1 = new Me();
  
        dad1.display();
        me1.display();
    }
}
输出
Inside show() method of Grandpa class
Inside show() method of Dad class
Inside show() method of Me class
Inside display() method of class B
Inside display() method of class C