Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Disclaimer:This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. Inheritance
  3. 3. InheritanceThe objectives of this chapter are:To explore the concept and implications of inheritanceTo define the syntax of inheritance in JavaTo understand the class hierarchy of JavaTo examine the effect of inheritance on constructors
  4. 4. TerminologyInheritance is a fundamental Object Oriented conceptA class can be defined as a "subclass" of another class. The subclass inherits all data attributes of its superclass The subclass inherits all methods of its superclass The subclass inherits all associations of its superclassThe subclass can: superclass: Person Add new functionality - name: String Use inherited functionality - dob: Date Override inherited functionality subclass: Employee - employeeID: int - salary: int - startDate: Date
  5. 5. What really happens?When an object is created using new, the system must allocate enoughmemory to hold all its instance variables. This includes any inherited instance variablesIn this example, we can say that an Employee "is a kind of" Person. An Employee object inherits all of the attributes, methods and associations of Person Person Person name = "John - name: String Smith" - dob: Date dob = Jan 13, 1954 Employee name = "Sally Halls" is a kind of dob = Mar 15, 1968 Employee employeeID = 37518 - employeeID: int salary = 65000 - salary: int startDate = Dec 15, - startDate: Date 2000
  6. 6. Inheritance in JavaInheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object public class Person Person { - name: String private String name; private Date dob; - dob: Date [...] public class Employee extends Person { Employee private int employeID; - employeeID: int private int salary; - salary: int private Date startDate; - startDate: Date [...] Employee anEmployee = new Employee();
  7. 7. Inheritance HierarchyEach Java class has one (and only one) superclass. C++ allows for multiple inheritanceInheritance creates a class hierarchy Classes higher in the hierarchy are more general and more abstract Classes lower in the hierarchy are more specific and concreteThere is no limit to the number of Classsubclasses a class can have Class Class ClassThere is no limit to the depth of theclass tree. Class Class Class Class
  8. 8. The class called ObjectAt the very top of the inheritance tree is a class called ObjectAll Java classes inherit from Object. All objects have a common ancestor This is different from C++The Object class is defined in the java.lang package Examine it in the Java API Specification Object
  9. 9. Object References and InheritanceInheritance defines "a kind of" relationship. In the previous example, OverdraftAccount "is a kind of" BankAccountBecause of this relationship, programmers can "substitute" objectreferences. A superclass reference can refer to an instance of the superclass OR an instance of ANY class which inherits from the superclass. BankAccount anAccount = new BankAccount(123456, "Craig"); BankAccount account1 = new OverdraftAccount(3323, "John", 1000.0); BankAccountanAccount name = "Craig" accountNumber = 123456 OverdraftAccount name = "John" accountNumber = account1 3323 limit = 1000.0
  10. 10. public class ChildClass extends arentClass{public void childMethod(){System.out.println("childMethods called from ChildClass");System.out.println("Calling parentMethod from ParentClass : ");parentMethod();}public static void main(String[] args){ChildClass child=new ChildClass();System.out.println("A simple java inheritance example : ");System.out.println("Calling childMethod from ClassClass : ");child.childMethod();}}
  11. 11. public class ParentClass {public void parentMethod(){System.out.println("parentMethod called fromParentClass");}}
  12. 12. A simple java inheritance example :Calling childMethod from ClassClass :childMethods called from ChildClassCalling parentMethod from ParentClass :parentMethod called from ParentClass
  13. 13. • If this presentation helped you, please visit our page and like it. Thanks in advance.• | |ww
  14. 14. Contact Us