340 likes | 511 Views
Java. Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut. Inheritance.
E N D
Java Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut
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
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 • To promote code reuse • To use polymorphism Powerd By: Arvind
Type of Inheritance Powerd By: Arvind
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
public static void main(String[] args) { Child obj=new Child();obj.set();obj.get(); }} Output: The Parent methodThe Child method Powerd By: Arvind
Output • Sports car Powerd By: Arvind
Why multiple inheritance is not supported in Java • To remove ambiguity. • To provide more maintainable and clear design. Powerd By: Arvind
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
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 class Parent{ String name; public void details() { name=“Parent”; System.out.println(“Parent”); }} Powerd By: Arvind
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 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
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
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 class Person{ String name;intage; void getdata(String a,int b) { name=a; age=b; }} Powerd By: Arvind
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
class guestTeacher extends Teacher{ float ppl; //pay per lecture void 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
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
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
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
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
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
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
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 class base_class_name{…………….…………….}class derived_class_name1 extends base_class_name{…………………………}class derived_class_name2 extends base_class_name{…………..…………..} Powerd By: Arvind
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
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