Disclaimer:This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocumen...
Inheritance        vineethmenon18@gmail.com
InheritanceThe objectives of this chapter are:To explore the concept and implications of inheritanceTo define the syntax o...
TerminologyInheritance is a fundamental Object Oriented conceptA class can be defined as a "subclass" of another class.  T...
What really happens?When an object is created using new, the system must allocate enoughmemory to hold all its instance va...
Inheritance in JavaInheritance is declared using the "extends" keyword  If inheritance is not defined, the class extends a...
Inheritance HierarchyEach Java class has one (and only one) superclass. C++ allows for multiple inheritanceInheritance cre...
The class called ObjectAt the very top of the inheritance tree is a class called ObjectAll Java classes inherit from Objec...
Object References and InheritanceInheritance defines "a kind of" relationship.  In the previous example, OverdraftAccount ...
public class ChildClass extends arentClass{public void childMethod(){System.out.println("childMethods called from ChildCla...
public class ParentClass {public void parentMethod(){System.out.println("parentMethod called fromParentClass");}}
A simple java inheritance example :Calling childMethod from ClassClass :childMethods called from ChildClassCalling parentM...
• If this presentation helped you, please visit  our page facebook.com/baabtra and like it.  Thanks in advance.• www.baabt...
Contact Us
Inheritance
Upcoming SlideShare
Loading in...5
×

Inheritance

351

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
351
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  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 vineethmenon18@gmail.com
  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 facebook.com/baabtra and like it. Thanks in advance.• www.baabtra.com | www.massbaab.com |ww w.baabte.com
  14. 14. Contact Us
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×