Java的接口和多态性
Java语言是所有编程语言中最流行的语言之一。使用Java编程语言有几个优点,无论是出于安全目的还是构建大型分发项目。使用JA的好处之一是Java试图借助类、继承、多态、接口等概念将语言中的每一个概念与现实世界联系起来。 在本文中,我们将讨论多态和接口概念.
多态性 是它有多种形式,这意味着一种特定的定义形式可以以多种不同的方式使用。现实生活中最简单的例子是假设我们必须存储此人的姓名和此人的电话号码,但是当一个人有两个不同的电话号码时,会有很多情况。我们必须以相同的名称保存相同的电话号码。
让我们在帮助下解释它。因此,在Java,可以使用面向对象的概念void insertPhone(String name, int phone)解决该问题。因此,此方法用于保存特定人员的电话号码。同样,我们可以使用相同的形式但不同的签名意味着不同的参数来存储此人的替代电话号码void insertPhone(String name, int phone1, int phone2)。一种方法有两种不同的形式并执行不同的操作。这是多态的一个例子,也就是方法重载。
Java的多态类型:
- 运行时多态
- 编译时多态
类型 1:运行 时间 多态性
这种类型的多态性是由Java虚拟机解决的,而不是由Java编译器解决的。这就是为什么这种类型的多态性被称为运行时多态性。运行时多态发生在Java中的方法覆盖期间。
例子
Java
// Java Program to Illustrate Run-time polymorphism
// Importing I/O classes
import java.io.*;
// Class 1 (Parent class)
class GFG1 {
//name method
void name() {
System.out.println("This is the GFG1 class");
}
}
// Class 2 (Child class)
// Main class extending parent class
public class GFG extends GFG1 {
// Method 1
void name() {
// Print statement
System.out.println("This is the GFG class");
}
// Method 2
// Main drive method
public static void main(String[] args) {
// Now creating 2 objects with different references and
// calling the Method 1 over the objects
// Case 1: GFG1 reference and GFG1 is the object
GFG1 ob = new GFG1();
ob.name();
// Case 2: GFG1 reference and GFG is the object
GFG1 ob1 = new GFG();
ob1.name();
}
}
Java
// Java Program to Illustrate Run-time polymorphism
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Helper class
class First {
// Method of this class
// Without any parameter
void check()
{
// Print statement if this method is called
System.out.println("This is the class First");
}
}
// Class 2
// Main class
class Second extends First {
// Method overloading
void check(String name)
{
// Printing the name of the class method having the
// parameter
System.out.println("This is the class " + name);
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Creating object of class 2
Second ob = new Second();
// Calling method over class 2 object
ob.check("Second");
// Creating object of class 1
First ob1 = new First();
ob.check();
// Upcasting
First ob2 = new Second();
ob.check();
}
}
Java
// Java Program to Demonstrate Concept of interfaces
// Interface
interface salary {
void insertsalary(int salary);
}
// Class 1
// Implementing the salary in the class
class SDE1 implements salary {
int salary;
@Override public void insertsalary(int salary)
{
this.salary = salary;
}
void printSalary() { System.out.println(this.salary); }
}
// Class 2
// Implementing the salary inside the SDE2 class
class SDE2 implements salary {
int salary;
@Override public void insertsalary(int salary)
{
this.salary = salary;
}
void printSalary() { System.out.println(this.salary); }
}
public class GFG {
public static void main(String[] args)
{
SDE1 ob = new SDE1();
// Insert different salaries
ob.insertsalary(100000);
ob.printSalary();
SDE2 ob1 = new SDE2();
ob1.insertsalary(200000);
ob1.printSalary();
}
}
This is the GFG1 class
This is the GFG class
输出说明:
在上面的例子中,同一个函数ie name 被调用了两次,但是在这两种情况下,输出是不同的。这些方法的签名也是一样的。这就是编译器无法确定应该执行哪个的原因。这仅在对象创建和类引用之后确定,这在运行时执行(内存管理)。这就是为什么这是运行时多态性。
类型 2:编译时多态
方法重载是编译时多态方法的一个例子。重载意味着具有相同名称但具有不同签名的函数。这是编译时多态性,因为这种类型的多态性是在编译时确定的,因为在编写代码时我们已经提到了相同函数名的不同类型的参数。
例子:
Java
// Java Program to Illustrate Run-time polymorphism
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Helper class
class First {
// Method of this class
// Without any parameter
void check()
{
// Print statement if this method is called
System.out.println("This is the class First");
}
}
// Class 2
// Main class
class Second extends First {
// Method overloading
void check(String name)
{
// Printing the name of the class method having the
// parameter
System.out.println("This is the class " + name);
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Creating object of class 2
Second ob = new Second();
// Calling method over class 2 object
ob.check("Second");
// Creating object of class 1
First ob1 = new First();
ob.check();
// Upcasting
First ob2 = new Second();
ob.check();
}
}
This is the class Second
This is the class First
This is the class First
接口与类非常相似。它们有变量和方法,但接口只允许抽象方法(不包含方法体),但是类和接口之间有什么区别?第一个优点是允许接口在特定类中实现多重继承。如果我们在类中扩展多个类, Java语言不支持多继承,但是在接口的帮助下, Java中允许多继承。
Real-life Example
The real-world example of interfaces is that we have multiple classes for different levels of employees working in a particular company and the necessary property of the class is the salary of the employees and this. We must be implemented in every class and. Also, it is different for every employee here. The concept of the interface is used. We simply create an interface containing an abstract salary method and implement it in all the classes and we can easily define different salaries of the employees.
例子:
Java
// Java Program to Demonstrate Concept of interfaces
// Interface
interface salary {
void insertsalary(int salary);
}
// Class 1
// Implementing the salary in the class
class SDE1 implements salary {
int salary;
@Override public void insertsalary(int salary)
{
this.salary = salary;
}
void printSalary() { System.out.println(this.salary); }
}
// Class 2
// Implementing the salary inside the SDE2 class
class SDE2 implements salary {
int salary;
@Override public void insertsalary(int salary)
{
this.salary = salary;
}
void printSalary() { System.out.println(this.salary); }
}
public class GFG {
public static void main(String[] args)
{
SDE1 ob = new SDE1();
// Insert different salaries
ob.insertsalary(100000);
ob.printSalary();
SDE2 ob1 = new SDE2();
ob1.insertsalary(200000);
ob1.printSalary();
}
}
100000
200000