更新時間:2022-10-14 10:35:12 來源:動力節點 瀏覽1585次
相信大家對Java繼承定義已經有所了解,先決條件: Java、Java 和多重繼承中的接口一個類可以擴展另一個類和/可以實現一個和多個接口。
例子:
// Java program to demonstrate that a class can
// implement multiple interfaces
import java.io.*;
interface intfA
{
void m1();
}
interface intfB
{
void m2();
}
// class implements both interfaces
// and provides implementation to the method.
class sample implements intfA, intfB
{
@Override
public void m1()
{
System.out.println("Welcome: inside the method m1");
}
@Override
public void m2()
{
System.out.println("Welcome: inside the method m2");
}
}
class GFG
{
public static void main (String[] args)
{
sample ob1 = new sample();
// calling the method implemented
// within the class.
ob1.m1();
ob1.m2();
}
}
輸出;
歡迎:在方法m1里面
歡迎:在方法m2里面
繼承就是將父類的屬性繼承到子類中。
Java 中的繼承是一種機制,其中一個對象獲取父對象的所有屬性和行為。
Java 繼承背后的理念是,您可以創建基于現有類的新類。從現有類繼承時,可以重用父類的方法和字段。
您還可以在當前類中添加新方法和字段。
繼承表示 IS_A 關系,也稱為父子關系。
例如:
狗 IS_A 動物
車 IS_A 車輛
員工 IS_A 人
外科醫生 IS_A 醫生等。
class Animal
{
public void eat()
{
}
}
Class Dog extends Animal
{
Public static void main(String args[])
{
Dog d=new Dog;
d.eat();
}
}
Java繼承的語法
類 <子類名稱> 擴展 <超類名稱>
{
//方法和字段
}
注意:extends 關鍵字表示您正在創建一個從現有類派生的新類。“擴展”的意思是增加功能。
例如_1:
import java.io.*;
class Person {
int id;
String name;
void set_Person()
{
try{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Id:");
id=Integer.parseInt(br.readLine());
System.out.println("Enter the Name");
name=br.readLine();
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Person()
{
System.out.print(id+"\t"+name+"\t");
}
}
class Employee extends Person{
int sal;
String desgn;
void set_Emp()
{
try{
set_Person();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Designation:");
desgn=br.readLine();
System.out.println("Enter the Salary:");
sal=Integer.parseInt(br.readLine());
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Emp()
{
disp_Person();
System.out.println(desgn+"\t"+sal);
}
public static void main(String args[])
{
Employee e1=new Employee();
e1.set_Emp();
e1.disp_Emp();
}
}
例如_2:
class Person1 {
int id;
String name;
void set_Person(int id,String name)
{
try{
this.id=id;
this.name=name;
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Person()
{
System.out.print(id+"\t"+name+"\t");
}
}
class Employee1 extends Person1 {
int sal;
String desgn;
void set_Emp(int id,String name,String desgn,int sal)
{
try{
set_Person(id,name);
this.desgn=desgn;
this.sal=sal;
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Emp()
{
disp_Person();
System.out.print(desgn+"\t"+sal);
}
public static void main(String args[])
{
Employee1 e1=new Employee1();
e1.set_Emp(1001,"Manjeet","AP",20000);
e1.disp_Emp();
}
}
Java 在java中支持三種類型的繼承:單級、多級和在類的情況下的分層繼承,以避免歧義。
在 Java 編程中,僅通過接口支持多重繼承和混合繼承。
單繼承示例
當一個類繼承另一個類時,稱??為單繼承。
class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b,product;
void set_B(int x)
{
b=x;
}
void cal_Product()
{
product=a*b;
System.out.println("Product ="+product);
}
public static void main(String[] args) {
B b=new B();
b.set_A(5);
b.set_B(5);
b.cal_Product();
}
}
多級繼承示例
當存在繼承鏈時,稱為多級繼承。
class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b;
void set_B(int x)
{
b=x;
}
}
class C extends B{
int c,product;
void cal_Product()
{
product=a*b;
System.out.println("Product ="+product);
}
public static void main(String[] args) {
C c=new C();
c.set_A(5);
c.set_B(5);
c.cal_Product();
}
}
層次繼承示例
當兩個或多個類繼承一個類時,稱??為層次繼承。
例如:
class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b;
void set_B(int x)
{
b=x;
}
}
class C extends A{
int c;
void set_C(int x)
{
c=x;
}
// Java program to demonstrate inheritance in
// interfaces.
import java.io.*;
interface intfA {
void geekName();
}
interface intfB extends intfA {
void geekInstitute();
}
// class implements both interfaces and provides
// implementation to the method.
class sample implements intfB {
@Override public void geekName()
{
System.out.println("Rohit");
}
@Override public void geekInstitute()
{
System.out.println("JIIT");
}
public static void main(String[] args)
{
sample ob1 = new sample();
// calling the method implemented
// within the class.
ob1.geekName();
ob1.geekInstitute();
}
}
輸出:
羅希特
JIIT
一個接口也可以擴展多個接口。
// Java program to demonstrate multiple inheritance
// in interfaces
import java.io.*;
interface intfA {
void geekName();
}
interface intfB {
void geekInstitute();
}
interface intfC extends intfA, intfB {
void geekBranch();
}
// class implements both interfaces and provides
// implementation to the method.
class sample implements intfC {
public void geekName() { System.out.println("Rohit"); }
public void geekInstitute()
{
System.out.println("JIIT");
}
public void geekBranch() { System.out.println("CSE"); }
public static void main(String[] args)
{
sample ob1 = new sample();
// calling the method implemented
// within the class.
ob1.geekName();
ob1.geekInstitute();
ob1.geekBranch();
}
}
輸出
羅希特
JIIT
CSE
為什么Java中的類不支持多重繼承,但通過接口可以實現?由于歧義,類不支持多重繼承。在接口的情況下,沒有歧義,因為方法的實現是由 Java 7 之前的實現類提供的。從 Java 8 開始,接口也有方法的實現。因此,如果一個類實現了兩個或多個具有相同方法簽名的接口,那么它也必須在類中實現該方法。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習