📜  Java中继承和组合的区别

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

Java中继承和组合的区别

继承
当我们想要创建一个新类并且已经有一个包含我们想要的一些代码的类时,我们可以从现有类派生我们的新类。这样做,我们可以重用现有类的字段和方法,而不必自己编写它们。

子类从其超类继承所有成员(字段、方法和嵌套类)。构造函数不是成员,所以子类不能继承,但是子类可以调用超类的构造函数。

继承类型有:

  1. 单继承
  2. 多级继承
  3. 多重继承
  4. 混合继承
  5. 分层继承

继承示例:

Java
class A {
    int a, b;
    public void add(int x, int y)
    {
        a = x;
        b = y;
        System.out.println(
            "addition of a + b is:"
            + (a + b));
    }
}
 
class B extends A {
    public void sum(int x, int y)
    {
        add(x, y);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        B b1 = new B();
        b1.sum(5, 6);
    }
}


Java
// Java program to illustrate
// the concept of Composition
 
import java.io.*;
import java.util.*;
 
// class book
class Book {
 
    public String title;
    public String author;
 
    Book(String title, String author)
    {
 
        this.title = title;
        this.author = author;
    }
}
 
// Library class contains
// list of books.
class Library {
 
    // reference to refer to the list of books.
    private final List books;
 
    Library(List books)
    {
        this.books = books;
    }
 
    public List getTotalBooksInLibrary()
    {
 
        return books;
    }
}
 
// main method
class GFG {
    public static void main(String[] args)
    {
 
        // Creating the Objects of Book class.
        Book b1 = new Book(
            "EffectiveJ Java",
            "Joshua Bloch");
        Book b2 = new Book(
            "Thinking in Java",
            "Bruce Eckel");
        Book b3 = new Book(
            "Java: The Complete Reference",
            "Herbert Schildt");
 
        // Creating the list which contains the
        // no. of books.
        List books = new ArrayList();
        books.add(b1);
        books.add(b2);
        books.add(b3);
 
        Library library = new Library(books);
 
        List bks = library.getTotalBooksInLibrary();
        for (Book bk : bks) {
 
            System.out.println("Title : "
                               + bk.title + " and "
                               + " Author : "
                               + bk.author);
        }
    }
}


输出:

addition of a+b is:11 

这里,B类是继承基类A的属性( add方法)的派生类。

组成
该组合还提供代码可重用性,但这里的区别是我们不为此扩展类。

组成示例:
让我们举个例子 图书馆

Java

// Java program to illustrate
// the concept of Composition
 
import java.io.*;
import java.util.*;
 
// class book
class Book {
 
    public String title;
    public String author;
 
    Book(String title, String author)
    {
 
        this.title = title;
        this.author = author;
    }
}
 
// Library class contains
// list of books.
class Library {
 
    // reference to refer to the list of books.
    private final List books;
 
    Library(List books)
    {
        this.books = books;
    }
 
    public List getTotalBooksInLibrary()
    {
 
        return books;
    }
}
 
// main method
class GFG {
    public static void main(String[] args)
    {
 
        // Creating the Objects of Book class.
        Book b1 = new Book(
            "EffectiveJ Java",
            "Joshua Bloch");
        Book b2 = new Book(
            "Thinking in Java",
            "Bruce Eckel");
        Book b3 = new Book(
            "Java: The Complete Reference",
            "Herbert Schildt");
 
        // Creating the list which contains the
        // no. of books.
        List books = new ArrayList();
        books.add(b1);
        books.add(b2);
        books.add(b3);
 
        Library library = new Library(books);
 
        List bks = library.getTotalBooksInLibrary();
        for (Book bk : bks) {
 
            System.out.println("Title : "
                               + bk.title + " and "
                               + " Author : "
                               + bk.author);
        }
    }
}

输出:

Title : EffectiveJ Java and  Author : Joshua Bloch
Title : Thinking in Java and  Author : Bruce Eckel
Title : Java: The Complete Reference and  Author : Herbert Schildt

继承和组合的区别:

S.NOInheritanceComposition
1.In inheritance, we define the class which we are inheriting(super class) and most importantly it cannot be changed at runtimeWhereas in composition we only define a type which we want to use and which can hold its different implementation also it can change at runtime. Hence, Composition is much more flexible than Inheritance.
2.Here we can only extend one class, in other words more than one class can’t be extended as java do not support multiple inheritance. 
 
Whereas composition allows to use functionality from different class.
3.In inheritance we need parent class in order to test child class.Composition allows to test the implementation of the classes we are using independent of parent or child class.
4.Inheritance cannot extend final class.Whereas composition allows code reuse even from final classes.
5.It is an is-a relationship.While it is a has-a relationship.