1 / 15

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Inheritance. Introduction. Inheritance Create new class from existing class Subclass extends super class. the keyword extends is used to derive a class in java. Simple and Multilevel Inheritance.

aloha
Télécharger la présentation

Object-Oriented Programming: Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming: Inheritance

  2. Introduction • Inheritance • Create new class from existing class • Subclass extends super class. the keyword extends is used to derive a class in java .

  3. Simple and Multilevel Inheritance Simple Inheritance Multilevel Inheritance

  4. Superclasses and subclasses • Example: • superclass: Vehicle • Cars, trucks, boats, bicycles, … • subclass: Car • Smaller, more-specific subset of vehicles

  5. single inheritance or one level inheritance class A {  int x;  int y;  int get(int p, int q){    x=p; y=q; return(0);    }    void Show(){      System.out.println(x);      }}class B extends A{  public static void main(String args[]){    A a = new A();    a.get(5,6);    a.Show();    }    void display(){      System.out.println("B");      }}

  6. Multilevel Inheritance class A {  int x;  int y;  int get(int p, int q){    x=p; y=q; return(0);    }    void Show(){      System.out.println(x);      }}class B extends A{  void Showb(){    System.out.println("B");    }}

  7. class C extends B{  void display(){    System.out.println("C");  }  public static void main(String args[]){    A a = new A();    a.get(5,6);    a.Show();    }}

  8. The super keyword The super is java keyword is used to access the members of the super class. It is used for two purposes in java. • keyword super used to access the hidden data variables of the super class hidden by the sub class. super.member; Here member can either be an instance variable or a method

  9. class A{  int a;  float b;  void Show(){    System.out.println("b in super class:  " + b);  }}class A{  int a;  float b;  void Show(){    System.out.println("b in super class:  " + b);  }}

  10. class B extends A{  int a;   float b;  B( int p, float q){    a = p;    super.b = q;  }  void Show(){    super.Show();    System.out.println("b in super class:  " + super.b);    System.out.println("a in sub class:    " + a);  }  public static void main(String[] args){    B subobj = new B(1, 5);    subobj.Show();  }}

  11. Output: b in super class: 5.0 b in super class: 5.0 a in sub class: 1

  12. 2. The keyword super used to call super class constructor in the subclass. This functionality can be achieved just by using the following command. super(param-list); Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor.  super()

  13. class A{  int a;  int b;  int c;  A(int p, int q, int r)class A{  int a;  int b;  int c;  A(int p, int q, int r) {    a=p;    b=q;    c=r;  }}

  14. class B extends A{    int d;    B(int l, int m, int n, int o){      super(l,m,n);      d=o;      }    void Show(){      System.out.println("a = " + a);      System.out.println("b = " + b);      System.out.println("c = " + c);      System.out.println("d = " + d);    }    public static void main(String args[]){      B b = new B(4,3,8,7);      b.Show();    }  }

  15. Output: a = 4 b = 3 c = 8 d = 7

More Related