Home > Java > javaTutorial > How to implement multiple inheritance of inner classes in Java

How to implement multiple inheritance of inner classes in Java

WBOY
Release: 2023-05-13 15:58:06
forward
862 people have browsed it

Explanation

1. Each internal class can be inherited by an (interface), so whether the external category has inherited an (interface) or not, it has no effect on the internal category. .

2. Without the capabilities provided by internal classes, you can inherit multiple concrete or abstract classes, and some design and programming problems will be difficult to solve.

The interface solves some problems. A class can implement multiple interfaces, and internal classes are allowed to inherit multiple non-interface types (categories or abstract classes).

Example

//类一
public class ClassA {
   public String name(){
       return "liutao";
   }
   public String doSomeThing(){
    // doSomeThing
   }
}
//类二
public class ClassB {
    public int age(){
        return 25;
    }
}
 
//类三
public class MainExample{
   private class Test1 extends ClassA{
        public String name(){
          return super.name();
        }
    }
    private class Test2 extends ClassB{
       public int age(){
         return super.age();
       }
    }
   public String name(){
    return new Test1().name();
   }
   public int age(){
       return new Test2().age();
   }
   public static void main(String args[]){
       MainExample mi=new MainExample();
       System.out.println("姓名:"+mi.name());
       System.out.println("年龄:"+mi.age());
   }
}
Copy after login

The above is the detailed content of How to implement multiple inheritance of inner classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template