Java
This presentation is the property of its rightful owner.
Sponsored Links
1 / 34

Java PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on
  • Presentation posted in: General

Java. Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut. Inheritance.

Download Presentation

Java

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java

Java

Powered By:

Arvind

Department of Computer science and engineering

Radha govind group of institutions,meerut


Inheritance

Inheritance

  • Inheritance provides a mechanism that allow a class to inherit property of another class when extends another class it inherits all non-private members including fields and methods.

  • Inheritance defines is a relationship between super class and sub class.

  • extends and implements keywords are used to describe inheritance In java.

Powerd By: Arvind


Java

Powerd By: Arvind


Java

class Car{

…………………

}

class Maruti extends Car{

……………………… //extends the property of car class

}

  • Car is a super class of maruti

  • Maruti is sub class of Car

  • Maruti is- a Car

Powerd By: Arvind


Purpose of inheritance

Purpose of inheritance

  • To promote code reuse

  • To use polymorphism

Powerd By: Arvind


Type of inheritance

Type of Inheritance

Powerd By: Arvind


Example of simple inheritance

Example of Simple inheritance

class Parent{public void get(){System.out.println(“The Parent method”);}}public class Child extends Parent{public void set(){System.out.println(“The Child method”);}}

Powerd By: Arvind


Java

public static void main(String[] args) {Child obj=new Child();obj.set();obj.get(); }}

Output:

The Parent methodThe Child method

Powerd By: Arvind


Java

Powerd By: Arvind


Output

Output

  • Sports car

Powerd By: Arvind


Why multiple inheritance is not supported in java

Why multiple inheritance is not supported in Java

  • To remove ambiguity.

  • To provide more maintainable and clear design.

Powerd By: Arvind


Java

Powerd By: Arvind


Super keyword

Super keyword

  • In java, super keyword is used to refer to immediate parent class of class.

  • In other words super keyword is used by a subclass whenever it need to refer to its immediate parent class.

Powerd By: Arvind


Java

Powerd By: Arvind


Example of child class referring parent class using super keyword

Example of Child class referring Parent class using super keyword

class Parent{String name;}public class Child extends Parent{

String name;public void details(){super.name=“Parent”;name=“Child”;System.out.println(super.name+“ and ”+name);}public static void main(String[] args){Child obj=new Child();obj.details();}}Output: Parent and Child

Powerd By: Arvind


Example of child class referring parent class methods using super keyword

Example of Child class referring Parent class methods using super keyword

class Parent{String name;public void details() {name=“Parent”;System.out.println(“Parent”);}}

Powerd By: Arvind


Java

public class Child extends Parent{

String name;public void details(){super. details();name=“Child”;System.out.println(name);}public static void main(String[] args){Child obj=new Child();obj.details();}}Output: Parent Child

Powerd By: Arvind


Example of child class calling parent class constructor using super keyword

Example of Child class calling Parent class constructor using super keyword

class Parent{String name;public parent(String n) {name=n; }}public class Child extends Parent{

public Child(String n1,String n2){super(n1);name=n2;}

}

Powerd By: Arvind


Java

public void details(){System.out.println(super.name+”and”+name);}public static void main(String[] args){Child obj=new Child(“Parent”,”child”);obj.details();}}

Output: Parent and Child

Powerd By: Arvind


Java

  • Can you use both this() and super() in a Constructor?

    Because both super() and this() must be first statement inside a constructor. Hence we cannot use them together.

Powerd By: Arvind


Multilevel inheritance

Multilevel Inheritance

class Person{String name;intage;void getdata(String a,int b) {name=a;age=b; }}

Powerd By: Arvind


Java

  • class Teacher extends Person{String qualification;float salary;void getqual(String s){qualification =s;}void getsal(float a){salary=a;}

    void display( ){System.out.println(“Name of teacher=”+name);System.out.println(“Age of teacher=”+age);System.out.println(“Qualification of teacher=”+qualification);System.out.println(“Salary of teacher=”+salary);}}

Powerd By: Arvind


Java

  • class guestTeacher extends Teacher{float ppl;  //pay per lecturevoid getppl(float a){ppl=a;}void display( ){System.out.println(“Name of teacher=”+name);System.out.println(“Age of teacher=”+age);System.out.println(“Qualification of teacher=”+qualification);System.out.println(“Payment per lecture=”+ppl);}}

Powerd By: Arvind


Java

  • class testInheritance{

  • public static void main(String args[ ]){Teacher teach=new Teacher( );teach.getdata(“Ajay”,50);teach.getqual(“Msc”);teach.getsal(1200);teach.display( );guestTeachergteach=new guestTeacher( );gteach.getdata(“Aman”,30);gteach.getqual(“Phd”);gteach.getppl(800);gteach.display( );}}

Powerd By: Arvind


Java

class Account { String cust_name; intacc_no; Account(String a, int b) {cust_name=a;

acc_no=b; }

void display() {

System.out.println ("Customer Name: "+cust_name);

System.out.println ("Account No: "+acc_no); } }

Powerd By: Arvind


Java

class Saving_Acc extends Account {

intmin_bal,

saving_bal;

Saving_Acc(String a, int b, int c, int d)

{

super(a,b);

min_bal=c;

saving_bal=d;

}

void display()

{

super.display();

System.out.println ("Minimum Balance: "+min_bal);

System.out.println ("Saving Balance: "+saving_bal);

}

}

Powerd By: Arvind


Java

class Acct_Details extends Saving_Acc

{

int deposits, withdrawals;

Acct_Details(String a, int b, int c, int d, int e, int f)

{

super(a,b,c,d);

deposits=e;

withdrawals=f;

}

void display()

{

super.display();

System.out.println ("Deposit: "+deposits);

System.out.println ("Withdrawals: "+withdrawals);

}

}

Powerd By: Arvind


Java

class q9Multilevel

{

public static void main(String args[])

{

Acct_Details A = new Acct_Details("Pa.one",666,1000,5000,500,9000);

A.display();

}

}

Powerd By: Arvind


Java

Class X

{

public void methodX()

{

System.out.println("Class X method");

}

}

Class Y extends X

{

public void methodY()

{

System.out.println("class Y method");

}

}

Powerd By: Arvind


Java

Class Z extends Y

{

public void methodZ()

{

System.out.println("class Z method");

}

public static void main(String args[])

{

Z obj = new Z();

obj.methodX(); //calling grand parent class method

obj.methodY(); //calling parent class method

obj.methodZ(); //calling local method

}

}

Powerd By: Arvind


Hierarchical inheritance

Hierarchical Inheritance

class base_class_name{…………….…………….}class derived_class_name1 extends base_class_name{…………………………}class derived_class_name2 extends base_class_name{…………..…………..}

Powerd By: Arvind


Java

  • class Base{

    void showBase(){System.out.println(“Base class method”);}}

    class Derived1 extends Base{void showDerived1(){System.out.println(“method of Derived1″);}}

Powerd By: Arvind


Java

class Derived2 extends Base{void showDerived2(){System.out.println(“method of Derived2″);}}

class TestHierarchical{public static void main(String args[]){Derived1 d1=new Derived1();Derived2 d2=new Derived2();d1.showDerived1();d1.showBase();d2.showDerived2();d2.showBase();}}

Powerd By: Arvind


Java

Powerd By: Arvind


  • Login