Prepared by: Sharifullah “Durrani”
Comsats institute of information Technology
Abbottabad Pakistan
© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.
 Inheritance
 A form of software reuse in which a new class is created by absorbing an existing
class’s members and embellishing them with new or modified capabilities.
 Can save time during program development by basing new classes on existing proven
and debugged high-quality software.
 Increases the likelihood that a system will be implemented and maintained effectively.
4
 Inheritance allows a software developer to
derive a new class from an existing one
 The existing class is called the parent class,
or superclass, or base class
 The derived class is called the child class or
subclass.
 As the name implies, the child inherits
characteristics of the parent
 That is, the child class inherits the methods
and data defined for the parent class
 When creating a class, rather than declaring completely new members, you can
designate that the new class should inherit the members of an existing class.
 Existing class is the superclass
 New class is the subclass
 Each subclass can be a superclass of future subclasses.
 A subclass can add its own fields and methods.
 A subclass is more specific than its superclass and represents a more specialized
group of objects.
 inheritance is sometimes referred to as specialization.
6
 Inheritance relationships often are shown
graphically in a UML class diagram, with an
arrow with an open arrowhead pointing to the
parent class
Inheritance should create an is-a relationship, meaning the child is a more
specific version of the parent
Vehicle
Car
7
 In Java, we use the reserved word extends to
establish an inheritance relationship
class Car extends Vehicle
{
// class contents
}
8
 Visibility modifiers determine which class members are
inherited and which are not
 Variables and methods declared with public visibility are
inherited; those with private visibility are not
 But public variables violate the principle of encapsulation
 There is a third visibility modifier that helps in inheritance
situations: protected
9
 The protected modifier allows a member of a
base class to be inherited into a child
 Protected visibility provides more
encapsulation than public visibility does
 However, protected visibility is not as tightly
encapsulated as private visibility
 Protected variables and methods can be shown
with a # symbol preceding them in UML
diagrams
10
 Constructors are not inherited, even though they have public
visibility
 Yet we often want to use the parent's constructor to set up
the "parent's part" of the object
 The super reference can be used to refer to the parent class,
and often is used to invoke the parent's constructor
 A child’s constructor is responsible for calling the parent’s
constructor
 The first line of a child’s constructor should use the super
reference to call the parent’s constructor
 The super reference can also be used to reference other
variables and methods defined in the parent’s class
 The direct superclass is the superclass from which the subclass explicitly
inherits.
 An indirect superclass is any class above the direct superclass in the class
hierarchy.
 The Java class hierarchy begins with class Object (in package
java.lang)
 Every class in Java directly or indirectly extends (or “inherits from”) Object.
 Java supports only single inheritance, in which each class is derived from
exactly one direct superclass.
 We distinguish between the is-a relationship and the has-a relationship
 Is-a represents inheritance
 In an is-a relationship, an object of a subclass can also be treated as an object of its
superclass
 Has-a represents composition
 In a has-a relationship, an object contains as members references to other objects
 Examples of superclasses and subclasses
 Superclasses tend to be “more general” and subclasses “more specific.”
 Because every subclass object is an object of its superclass, and one
superclass can have many subclasses,
 The set of objects represented by a superclass is typically larger than the set
of objects represented by any of its subclasses.
A superclass exists in a hierarchical relationship with its subclasses.
Each arrow in the hierarchy represents an is-a relationship.
an Employee is a CommunityMember”
“a Teacher is a Faculty member.”
Superclasses and Subclasses
Shape inheritance hierarchy
A Triangle is a TwoDimensionalShape and is a
Shape
 Not every class relationship is an inheritance relationship.
 Has-a relationship
 Create classes by composition of existing classes.
 Example: Given the classes Employee, BirthDate and TelephoneNumber, it’s
improper to say that an Employee is a BirthDate or that an Employee is a
TelephoneNumber.
 However, an Employee has a BirthDate, and an Employee has a
TelephoneNumber.
 Objects of all classes that extend a common superclass can be treated as
objects of that superclass.
 Commonality expressed in the members of the superclass.
 Inheritance issue
 A subclass can inherit methods that it does not need or should not have.
 Even when a superclass method is appropriate for a subclass, that subclass often needs
a customized version of the method.
 The subclass can override (redefine) the superclass method with an appropriate
implementation.
 A class’s public members are accessible wherever the program has a reference
to an object of that class or one of its subclasses.
 A class’s private members are accessible only within the class itself.
 protected access is an intermediate level of access between public and
private.
 A superclass’s protected members can be accessed by members of that superclass, by
members of its subclasses and by members of other classes in the same package
 protected members also have package access.
 All public and protected superclass members retain their original access modifier when
they become members of the subclass.
 A superclass’s private members are hidden in its subclasses
 They can be accessed only through the public or protected methods inherited from the
superclass
 Subclass methods can refer to public and protected members inherited
from the superclass simply by using the member names.
 When a subclass method overrides an inherited superclass method, the superclass
method can be accessed from the subclass by preceding the superclass method
name with keyword super and a dot (.) separator.
 Java supports single inheritance, meaning
that a derived class can have only one parent
class
 Multiple inheritance allows a class to be
derived from two or more classes, inheriting
the members of all parents
 Collisions, such as the same variable name in
two parents, have to be resolved
 Java does not support multiple inheritance
 In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
24
 A child class can override the definition of an inherited
method in favor of its own
 The new method must have the same signature as the
parent's method, but can have a different body
 The type of the object executing the method determines
which version of the method is invoked
 A parent method can be invoked explicitly using the super
reference
 If a method is declared with the final modifier, it cannot be
overridden
 The concept of overriding can be applied to data and is called
shadowing variables
 Shadowing variables should be avoided because it tends to
cause unnecessarily confusing code
26
 Don't confuse the concepts of overloading and
overriding
 Overloading deals with multiple methods with the
same name in the same class, but with different
signatures
 Overriding deals with two methods, one in a parent
class and one in a child class, that have the same
signature
 Overloading lets you define a similar operation in
different ways for different data
 Overriding lets you define a similar operation in
different ways for different object types
Assign the base class reference to a child
class object.
 the object can call the overriding methods of child class and
all the non-overridden methods of base class
 but it cannot call the methods which are newly declared in
the child class
 Argument list: The argument list of overriding method must
be same as that of the method in parent class.
 The data types of the arguments and their sequence should
be maintained as it is in the overriding method.
 Access Modifier: The Access Modifier of the overriding
method (method of subclass) cannot be more restrictive than
the overridden method of parent class.
 This is not
allowed as child
class disp
method is more
restrictive(protect
ed) than base
class(public)
 Its valid
 private, static and final methods cannot be
overridden as they are local to the class.
 However static methods can be re-declared in
the sub class.
 Binding of overridden methods happen at
runtime which is known as dynamic binding.
 super keyword is used for calling the parent
class method/constructor.
 super keyword
is used for
calling the
parent class
method/constr
uctor.
36
 A class called Object is defined in the java.lang package of
the Java standard class library
 All classes are derived from the Object class
 If a class is not explicitly defined to be the child of an existing
class, it is assumed to be the child of the Object class
 Therefore, the Object class is the ultimate root of all class
hierarchies
 The Object class contains a few useful methods, which are inherited
by all classes
 For example, the toString method is defined in the Object class
 Every time we have defined toString, we have actually been
overriding an existing definition
 The toString method in the Object class is defined to return a
string that contains the name of the object’s class together along with
some other information
 All objects are guaranteed to have a toString method via
inheritance
 Thus the println method can call toString for any object
that is passed to it

Inheritance in java

  • 1.
    Prepared by: Sharifullah“Durrani” Comsats institute of information Technology Abbottabad Pakistan © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.
  • 3.
     Inheritance  Aform of software reuse in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities.  Can save time during program development by basing new classes on existing proven and debugged high-quality software.  Increases the likelihood that a system will be implemented and maintained effectively.
  • 4.
    4  Inheritance allowsa software developer to derive a new class from an existing one  The existing class is called the parent class, or superclass, or base class  The derived class is called the child class or subclass.  As the name implies, the child inherits characteristics of the parent  That is, the child class inherits the methods and data defined for the parent class
  • 5.
     When creatinga class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class.  Existing class is the superclass  New class is the subclass  Each subclass can be a superclass of future subclasses.  A subclass can add its own fields and methods.  A subclass is more specific than its superclass and represents a more specialized group of objects.  inheritance is sometimes referred to as specialization.
  • 6.
    6  Inheritance relationshipsoften are shown graphically in a UML class diagram, with an arrow with an open arrowhead pointing to the parent class Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Vehicle Car
  • 7.
    7  In Java,we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents }
  • 8.
    8  Visibility modifiersdetermine which class members are inherited and which are not  Variables and methods declared with public visibility are inherited; those with private visibility are not  But public variables violate the principle of encapsulation  There is a third visibility modifier that helps in inheritance situations: protected
  • 9.
    9  The protectedmodifier allows a member of a base class to be inherited into a child  Protected visibility provides more encapsulation than public visibility does  However, protected visibility is not as tightly encapsulated as private visibility  Protected variables and methods can be shown with a # symbol preceding them in UML diagrams
  • 10.
    10  Constructors arenot inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
  • 11.
     A child’sconstructor is responsible for calling the parent’s constructor  The first line of a child’s constructor should use the super reference to call the parent’s constructor  The super reference can also be used to reference other variables and methods defined in the parent’s class
  • 12.
     The directsuperclass is the superclass from which the subclass explicitly inherits.  An indirect superclass is any class above the direct superclass in the class hierarchy.  The Java class hierarchy begins with class Object (in package java.lang)  Every class in Java directly or indirectly extends (or “inherits from”) Object.  Java supports only single inheritance, in which each class is derived from exactly one direct superclass.
  • 13.
     We distinguishbetween the is-a relationship and the has-a relationship  Is-a represents inheritance  In an is-a relationship, an object of a subclass can also be treated as an object of its superclass  Has-a represents composition  In a has-a relationship, an object contains as members references to other objects
  • 14.
     Examples ofsuperclasses and subclasses  Superclasses tend to be “more general” and subclasses “more specific.”  Because every subclass object is an object of its superclass, and one superclass can have many subclasses,  The set of objects represented by a superclass is typically larger than the set of objects represented by any of its subclasses.
  • 16.
    A superclass existsin a hierarchical relationship with its subclasses. Each arrow in the hierarchy represents an is-a relationship. an Employee is a CommunityMember” “a Teacher is a Faculty member.” Superclasses and Subclasses
  • 17.
    Shape inheritance hierarchy ATriangle is a TwoDimensionalShape and is a Shape
  • 18.
     Not everyclass relationship is an inheritance relationship.  Has-a relationship  Create classes by composition of existing classes.  Example: Given the classes Employee, BirthDate and TelephoneNumber, it’s improper to say that an Employee is a BirthDate or that an Employee is a TelephoneNumber.  However, an Employee has a BirthDate, and an Employee has a TelephoneNumber.
  • 19.
     Objects ofall classes that extend a common superclass can be treated as objects of that superclass.  Commonality expressed in the members of the superclass.  Inheritance issue  A subclass can inherit methods that it does not need or should not have.  Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method.  The subclass can override (redefine) the superclass method with an appropriate implementation.
  • 20.
     A class’spublic members are accessible wherever the program has a reference to an object of that class or one of its subclasses.  A class’s private members are accessible only within the class itself.  protected access is an intermediate level of access between public and private.  A superclass’s protected members can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package  protected members also have package access.  All public and protected superclass members retain their original access modifier when they become members of the subclass.
  • 21.
     A superclass’sprivate members are hidden in its subclasses  They can be accessed only through the public or protected methods inherited from the superclass  Subclass methods can refer to public and protected members inherited from the superclass simply by using the member names.  When a subclass method overrides an inherited superclass method, the superclass method can be accessed from the subclass by preceding the superclass method name with keyword super and a dot (.) separator.
  • 22.
     Java supportssingle inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Collisions, such as the same variable name in two parents, have to be resolved  Java does not support multiple inheritance  In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
  • 24.
    24  A childclass can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 25.
     A parentmethod can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 26.
    26  Don't confusethe concepts of overloading and overriding  Overloading deals with multiple methods with the same name in the same class, but with different signatures  Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature  Overloading lets you define a similar operation in different ways for different data  Overriding lets you define a similar operation in different ways for different object types
  • 28.
    Assign the baseclass reference to a child class object.
  • 30.
     the objectcan call the overriding methods of child class and all the non-overridden methods of base class  but it cannot call the methods which are newly declared in the child class
  • 31.
     Argument list:The argument list of overriding method must be same as that of the method in parent class.  The data types of the arguments and their sequence should be maintained as it is in the overriding method.  Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class.
  • 32.
     This isnot allowed as child class disp method is more restrictive(protect ed) than base class(public)
  • 33.
  • 34.
     private, staticand final methods cannot be overridden as they are local to the class.  However static methods can be re-declared in the sub class.  Binding of overridden methods happen at runtime which is known as dynamic binding.  super keyword is used for calling the parent class method/constructor.
  • 35.
     super keyword isused for calling the parent class method/constr uctor.
  • 36.
    36  A classcalled Object is defined in the java.lang package of the Java standard class library  All classes are derived from the Object class  If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class  Therefore, the Object class is the ultimate root of all class hierarchies
  • 37.
     The Objectclass contains a few useful methods, which are inherited by all classes  For example, the toString method is defined in the Object class  Every time we have defined toString, we have actually been overriding an existing definition  The toString method in the Object class is defined to return a string that contains the name of the object’s class together along with some other information
  • 38.
     All objectsare guaranteed to have a toString method via inheritance  Thus the println method can call toString for any object that is passed to it