1 / 34

Java

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

walker
Télécharger la présentation

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. 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. Java Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut

  2. 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

  3. Powerd By: Arvind

  4. 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

  5. Purpose of inheritance • To promote code reuse • To use polymorphism Powerd By: Arvind

  6. Type of Inheritance Powerd By: Arvind

  7. 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

  8. public static void main(String[] args) { Child obj=new Child();obj.set();obj.get(); }} Output: The Parent methodThe Child method Powerd By: Arvind

  9. Powerd By: Arvind

  10. Output • Sports car Powerd By: Arvind

  11. Why multiple inheritance is not supported in Java • To remove ambiguity. • To provide more maintainable and clear design. Powerd By: Arvind

  12. Powerd By: Arvind

  13. 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

  14. Powerd By: Arvind

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. Multilevel Inheritance class Person{ String name;intage; void getdata(String a,int b) { name=a; age=b; }} Powerd By: Arvind

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. Powerd By: Arvind

More Related