SlideShare a Scribd company logo
1 of 15
Disclaimer:This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Inheritance



        vineethmenon18@gmail.com
Inheritance

The objectives of this chapter are:

To explore the concept and implications of inheritance

To define the syntax of inheritance in Java
To understand the class hierarchy of Java
To examine the effect of inheritance on constructors
Terminology

Inheritance is a fundamental Object Oriented concept

A 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 superclass

The 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
What really happens?

When an object is created using new, the system must allocate enough
memory to hold all its instance variables.
 This includes any inherited instance variables

In 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
Inheritance in Java

Inheritance 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();
Inheritance Hierarchy

Each Java class has one (and only one) superclass.
 C++ allows for multiple inheritance

Inheritance creates a class hierarchy
  Classes higher in the hierarchy are more general and more abstract
  Classes lower in the hierarchy are more specific and concrete

There is no limit to the number of                        Class
subclasses a class can have
                                                  Class    Class       Class
There is no limit to the depth of the
class tree.
                                          Class            Class       Class




                                          Class
The class called Object

At the very top of the inheritance tree is a class called Object

All 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
Object References and Inheritance

Inheritance defines "a kind of" relationship.
  In the previous example, OverdraftAccount "is a kind of" BankAccount

Because of this relationship, programmers can "substitute" object
references.
  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);


                     BankAccount
anAccount            name = "Craig"
                     accountNumber = 123456                       OverdraftAccount
                                                                  name = "John"
                                                                  accountNumber =
                                    account1                      3323
                                                                  limit = 1000.0
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();
}
}
public class ParentClass {
public void parentMethod()
{
System.out.println("parentMethod called from
ParentClass");
}
}
A simple java inheritance example :
Calling childMethod from ClassClass :
childMethods called from ChildClass
Calling parentMethod from ParentClass :
parentMethod called from ParentClass
• 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
Contact Us

More Related Content

What's hot

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming Raghunath A
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)Shambhavi Vats
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-OrientedOmid AmirGhiasvand
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-conceptsraahulwasule
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Dr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java InheritanceDr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java Inheritancejalinder123
 
oops -concepts
oops -conceptsoops -concepts
oops -conceptssinhacp
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in PythonSujith Kumar
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Lightning talk
Lightning talkLightning talk
Lightning talknpalaniuk
 

What's hot (20)

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-Oriented
 
Only oop
Only oopOnly oop
Only oop
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Dr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java InheritanceDr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java Inheritance
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
 
java classes
java classesjava classes
java classes
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
OOPS
OOPSOOPS
OOPS
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 

Similar to Inheritance

Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingshinyduela
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java yash jain
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptxVishwanathanS5
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptRithwikRanjan
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)Jyoti shukla
 
Inheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptxInheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptxABHINAVARYANCSEA301
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیMohammad Reza Kamalifard
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 

Similar to Inheritance (20)

Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Python advance
Python advancePython advance
Python advance
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
Inheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptxInheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptx
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Inheritance

  • 1.
  • 2. Disclaimer:This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3. Inheritance vineethmenon18@gmail.com
  • 4. Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance To define the syntax of inheritance in Java To understand the class hierarchy of Java To examine the effect of inheritance on constructors
  • 5. Terminology Inheritance is a fundamental Object Oriented concept A 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 superclass The 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
  • 6. What really happens? When an object is created using new, the system must allocate enough memory to hold all its instance variables. This includes any inherited instance variables In 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
  • 7. Inheritance in Java Inheritance 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();
  • 8. Inheritance Hierarchy Each Java class has one (and only one) superclass. C++ allows for multiple inheritance Inheritance creates a class hierarchy Classes higher in the hierarchy are more general and more abstract Classes lower in the hierarchy are more specific and concrete There is no limit to the number of Class subclasses a class can have Class Class Class There is no limit to the depth of the class tree. Class Class Class Class
  • 9. The class called Object At the very top of the inheritance tree is a class called Object All 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
  • 10. Object References and Inheritance Inheritance defines "a kind of" relationship. In the previous example, OverdraftAccount "is a kind of" BankAccount Because of this relationship, programmers can "substitute" object references. 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); BankAccount anAccount name = "Craig" accountNumber = 123456 OverdraftAccount name = "John" accountNumber = account1 3323 limit = 1000.0
  • 11. 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(); } }
  • 12. public class ParentClass { public void parentMethod() { System.out.println("parentMethod called from ParentClass"); } }
  • 13. A simple java inheritance example : Calling childMethod from ClassClass : childMethods called from ChildClass Calling parentMethod from ParentClass : parentMethod called from ParentClass
  • 14. • 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