更新時(shí)間:2022-10-14 10:35:12 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1606次
相信大家對(duì)Java繼承定義已經(jīng)有所了解,先決條件: Java、Java 和多重繼承中的接口一個(gè)類可以擴(kuò)展另一個(gè)類和/可以實(shí)現(xiàn)一個(gè)和多個(gè)接口。
例子:
// 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 中的繼承是一種機(jī)制,其中一個(gè)對(duì)象獲取父對(duì)象的所有屬性和行為。
Java 繼承背后的理念是,您可以創(chuàng)建基于現(xiàn)有類的新類。從現(xiàn)有類繼承時(shí),可以重用父類的方法和字段。
您還可以在當(dāng)前類中添加新方法和字段。
繼承表示 IS_A 關(guān)系,也稱為父子關(guān)系。
例如:
狗 IS_A 動(dòng)物
車 IS_A 車輛
員工 IS_A 人
外科醫(yī)生 IS_A 醫(yī)生等。
class Animal
{
public void eat()
{
}
}
Class Dog extends Animal
{
Public static void main(String args[])
{
Dog d=new Dog;
d.eat();
}
}
Java繼承的語(yǔ)法
類 <子類名稱> 擴(kuò)展 <超類名稱>
{
//方法和字段
}
注意:extends 關(guān)鍵字表示您正在創(chuàng)建一個(gè)從現(xiàn)有類派生的新類。“擴(kuò)展”的意思是增加功能。
例如_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中支持三種類型的繼承:?jiǎn)渭?jí)、多級(jí)和在類的情況下的分層繼承,以避免歧義。
在 Java 編程中,僅通過(guò)接口支持多重繼承和混合繼承。
單繼承示例
當(dāng)一個(gè)類繼承另一個(gè)類時(shí),稱??為單繼承。
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();
}
}
多級(jí)繼承示例
當(dāng)存在繼承鏈時(shí),稱為多級(jí)繼承。
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();
}
}
層次繼承示例
當(dāng)兩個(gè)或多個(gè)類繼承一個(gè)類時(shí),稱??為層次繼承。
例如:
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
一個(gè)接口也可以擴(kuò)展多個(gè)接口。
// 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中的類不支持多重繼承,但通過(guò)接口可以實(shí)現(xiàn)?由于歧義,類不支持多重繼承。在接口的情況下,沒(méi)有歧義,因?yàn)榉椒ǖ膶?shí)現(xiàn)是由 Java 7 之前的實(shí)現(xiàn)類提供的。從 Java 8 開始,接口也有方法的實(shí)現(xiàn)。因此,如果一個(gè)類實(shí)現(xiàn)了兩個(gè)或多個(gè)具有相同方法簽名的接口,那么它也必須在類中實(shí)現(xiàn)該方法。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743