Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

03 Inheritance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

JAVA PROGRAMMING

INHERITANCE.

 Inheritance
 Types of Inheritance, single Inheritance, multilevel Inheritance, Hierarchical Inheritance.
 Method & Constructor Overloading & Overriding,
 Dynamic Method Dispatch
 Final variables, final methods, final class.
 Use of super keyword
 Abstract methods & classes,
 Static members and Methods.

Reusability of code is the most important aspect of OOP paradigm. It is always best practice to reuse something that
already exists rather than creating the same all over again.
INHERITANCE
 Inheritance is the mechanism of deriving new class from old class. The old class is knows as superclass or
base class or parent class.The new class is known as subclass or derived class or child class.
 The subclass inherits all of its instances variables and methods defined by the superclass and Sub class also
adds its own unique elements like instances variables and methods.
 Thus we can say that, subclass are specialized version of superclass.
Rules about Inheritance.
 Inheritance and different visibility modifires (private/public/protected/friendly)
o All public variables of Super class will be inherited by subclass.
o Private member can not be inherited by subclass because it will not be visible to subclass.
o Protected members of Super class will be inherited by subclass as protected. So these mebers can
directly visible in Subclass methods but not accessible by object of Subclass.
o All default variables (friendly) will be inherited by all subclass.
 Inheritance and Constructor
o Constructor is not inherited by Sub class.
o The default constructor of Super Class is autometically invoked when Sub Class object is created.
o The parameterised constructor of Super class is invoked from the Sub class constructor using super()
method.
 Inheritance and Package
o All default variables (friendly) will be inherited by all subclass in the same package only. Subclass
outside the package will not inherit any default memeber.
o Protected variable will be inherited by all subclass in the same package or outside package. (Different
from default).

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 1 of 10


o Public variable will be inherited by all subclass in the same package or outside package. And these are
directly accessible by object.
 Inheritance and Overriding
o Methods which are not inherited (private) can not be overriden.
o But Methods can still be defined in subclass but those methods will not be overriden method but a new
method.
o The final method of super class will not be overriden to the subclass.
o The Abstact method must be inherited to the subclass.
 Inheritance and Static Variables/Methods
o Static methods /variables do not take part in inheritance.
o Even static methods / variables do not take part in inheritance and can not be overriden.
o But they can be redefined in subclass. Redefinition is not called overriden but hidden.
 Inheritance and extends keyword
o We use extends keyword to inherits Super Class.
o The keyword extends only extends one class.
 Types of Inheritance with classes
o Single Inheritance
o Hirarchical Inheritance
o Multilevel Inheritance
 Types of Inheritance with interfaces
o Multiple Inheritance
Benefits of java’s Inheritance
 Reusability of code
 Code of sharing
 Consistancy in using an interface
Superclass (Base Class)
 It is a class from which other Class can be derived.
Subclass (Child Class)
 It is a Class that inherit some or all member.
 A subclass can add new private instance variables.
 A subclass can add new public, private or static methods.
 A subclass can override inherited methods.
 A subclass may not redefine a public method as private.
 A subclass may not override static methods of the superclass.
 A subclass must define its own constructors.
 A subclass cannot access the private members of its superclass.

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 2 of 10


TYPES OF INHERITANCE IN JAVA
Single Inheritance:-
 Single level inheritance is that in which there is only one super class.
 It is the mechanism of deriving a single subclass from the Superclass.
 All the properties of superclass are extended to the subclass except private
properties.
 The Sub class have its own data members ,methods and also have methods
inherited from super class.
 Syntax of Single Inheritance is as follow.
class SuperClass
{
// Instance variable(s) declaration;
//Constructor(s) defination;
//Instance method(s) defination;
}
class SubClass extends SuperClass
{
//Instance variable(s) declaration of SubClass;
//Constructor(s) defination of SubClass;
//Instance method(s) defination of SubClass;
}

 Example of Single Inheritance is as follow.


class Person
{
private String pname;
private int page;
void setPersonData(String name, int age)
{
pname = name;
page = age;
}
void showPersonData()
{
System.out.println(“Person Name Is =>> “ + Pname);
System.out.println(“Person Age Is =>> “ + page);
}
}
class Employee extends Person
{
private String epost;
private int esal;
void setEmployeeData(String post, int sal)
{
epost = post;
esal = age;
}
void showEmployeeData()
{
System.out.println(“Employee Post Is =>> “ + epost);
System.out.println(“Employee Salary Is =>> “ + esal);

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 3 of 10


}
}
//Create a class and Save it with name SingleInheritanceDemo.java
class SingleInheritanceDemo
{
Public static void main(String args[])
{
Employee E = new Employee(); //Object of Sub Class is Created
E. setPersonData(“Nitin Shah Sir”, 40 ); //Call Super class method
E. setEmployeeData(“Director”,20000); //Call Sub Class method
E. showPersonData(); //method to Show super class information
E. showEmployeeData(); //method to Show sub class information
}
}

Hirarchical Inheritance :
 One Class is extended by many Subclasses. It is One-to-many relationship.
 Hirarchical inheritance is that, in which there is only one super class for multiple subclasses.
 It is the mechanism of deriving a multiple subclasss from the Superclass.
 All the properties of superclass are extended to all subclasss except private properties.
 The Sub classes also have its own data members ,methods and also have methods inherited from super class.
 Syntax of Hirarchical Inheritance is as follow.
class SuperClass
{
// Instance variable(s) declaration;
//Constructor(s) defination;
//Instance method(s) defination;
}
class SubClass_1 extends SuperClass
{
//Instance variable(s) declaration of SubClass_1;
//Constructor(s) defination of SubClass_1;
//Instance method(s) defination of SubClass_1;
}
class SubClass_2 extends SuperClass
{
//Instance variable(s) declaration of
SubClass_2;
//Constructor(s) defination of SubClass_2;
//Instance method(s) defination of SubClass_2;
}

 Example of Hirarchical Inheritance is as follow.


Class Person
{
//.. Define as per diagram ….
}
Class Employee extends Person
{
//.. Define as per diagram ….
}

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 4 of 10


Class Student extends Person
{
//.. Define as per diagram ….
}
Class HirarchicalInheritanceDemo
{
Public static void main(String args[])
{
//Create Objects
Employee E = new Employee();
E. setPersonData(“Nitin Shah Sir”, 40 ); //Call Super class method
E. setEmployeeData(“Director”,20000); //Call Sub Class method
E. showPersonData(); //method to Show super class information
E. showEmployeeData(); //method to Show sub class information
Student S = new Student();
S. setPersonData(“Rahul Patil”, 20 ); //Call Super class method
S. setStudentData(“Computer”,5); //Call Sub Class method
S. showPersonData(); //method to Show super class information
S. showStudentData(); //method to Show sub class information
}
}

Multilevel Inheritance:
 One Class is extended by Subclass_1 and Subclass_1 is extended by Subclass_2 and
so on.
 Multilevel inheritance is that, in which Super class A is extended by Subclass B and
then further Subclass B is (Super Class for C) extended by Subclass C. Then this whole
structure is called as Multilevel Inheritance.
 It is ladder or hierarchy of single level inheritance.
 Multiple Classes are involved in this inheritance, but one class extends by only one
class.
 The lowermost Subclass can make use of all it super classes members.
 All the properties of superclass A are extended to B, and properties of class B (including inherited from class A)
are extended to class C except private properties of A and B.
 Each Sub classes also have its own data members, methods and also have methods inherited from its super
class.
 Syntax of Multilevel Inheritance is as follow.
class SuperClass
{
// Instance variable(s) declaration;
//Constructor(s) defination;
//Instance method(s) defination;
}
class SubClass_1 extends SuperClass
{
//Instance variable(s) declaration of SubClass_1;
//Constructor(s) defination of SubClass_1;
//Instance method(s) defination of SubClass_1;
}

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 5 of 10


class SubClass_2 extends SubClass_1
{
//Instance variable(s) declaration of SubClass_2;
//Constructor(s) defination of SubClass_2;
//Instance method(s) defination of SubClass_2;
}

INHERITANCE AND METHOD OVERLOADING


 We can overload the methods in Super class as well as in Sub class, so all overloaded methods of Superclass
are inherited to sub class except private methods.
INHERITANCE AND CONSTRUCTOR
 Default Constructor and Inheritance..
o The default constructor of super class are invoked autometically from the default constructor of Sub
class, when object is created.
o We can also call super class constructor by calling super() method, which is add as a first line in Sub
Class Constructor.
o Example :
Class SubClass extends SuperClass
{
SubClass()
{
Super(); //make a call of super class constructor
//Further Initialisation of Sub Class members
}
}
o Note : Super class constructor is executed before sub class constructor.
 Parameterised Constructor and Inheritance..
o The parameterised constructor of super class are invoked from the constructor of Sub class, when
object is created.
o We can pass the parameters to parameterised constructor at a time of object creation.
o We can also call super class parameterised constructor by calling super( parameter ) method (by
passing the values to super class constructor).
o Super( parameter ) is add as a first line in Sub Class Constructor.
o Example :
Class SubClass extends SuperClass
{
SubClass( list of parameters)
{
Super( pass parameters for super class );
//Further Initialisation of Sub Class members
}
}
o Note : Super class constructor is executed before sub class constructor.
o
METHNOD OVERIDING

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 6 of 10


 Method overriding is only implemented in inheritance.
 We can only override the method that it is not marked as final in super class.
 Method overriding is the ability to define a superclass method in subclass identically with its return type, method
name and argument list.
 It means a subclass can implements a parent Class method baseed on it’s requirement.
 Overriding method can have same or heigher access modifier than method being overriden.
 Final method can not be overriden.
 Static methods can not be overriden.
 If a method cannot be inherited (private) then it cannot be overriden.
 Method Overriding is used to provide specific implementation of a method that is already provided by its super
class.
 Method Overriding is used for Runtime Polymorphism
 Super keyword is used to invoke the overridden method of super class.
 Example :
class SuperClass
{
void show()
{
System.out.println(“Super Class Method : ”);
}
}
class SubClass extends SuperClass
{
void show()
{
Super.show();
System.out.println(“Overrided Method in Sub Class”);
}
}
 Define class and main method() , create object of Subclass and call show() method with object.
DYNAMIC METHOD DISPATCH
 Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time,
rather than compile time.
 Dynamic method dispatch is important because this is how java implements run-time polymorphism.
 Method to execution based upon the type of the object being reffered to at the time the call occurs.
 Thus, this determination is made at run time.
 In other words, it is the type of object being referred to (not the type of the reference variable) that determines
which version of an overridden method will be executed.
 Example of Dynamic Method Dispatch
class SuperClass
{
void sum(int x,int y)
{
System.out.println(“Super Class Addition is : ” + (x+y));

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 7 of 10


}
}
class SubClass extends SuperClass
{
//Override sum method of Super Class
void sum(int x,int y)
{
System.out.println(“Sub Class Addition is : ” + (x+y));
}
}

class DMDDemo
{
public static void main( String args[] )
{
SuperClass A = new SuperClass(); //Super class object is created
SubClass B = new SubClass(); //Sub class object is created
SuperClass C; //Super class Referance is created
C=A; //Now Referance C contains object of Super Class
C.sum(10,20); //Super class method Dynamically Executed
C=B; //Now Referance C contains object of Sub Class
C.sum(10,20); //Sub class method Dynamically Executed
}
}

FINAL VARIABLES, FINAL METHODS, FINAL CLASS.


Final keyword can be used in the following ways
 Final Variable :
o Once a variable is declared as final, its value can not be changed during the scope of the program.
o It create named or symbolic constant.
o These are just like a class variable and do not take the space for every object.
o They must be initialise at a time of declaration.
o We can’t change the values of these variable during their life.
o Syntax : final datatype variable = value;
o Example : final int maxmarks=100;
 Final Method :
o Method declare as final can not be overridden.
o It prevents overriding of method.
o To disallow a method to be overridden final can be used as a modifier at the start of declaration.
o The functionality of method declare as a final is not altered in any case.
o Any attempt to override final methods will cause an error.
o Syntax : final returntype methodname( list of parameters ) // In class.
 Final Class :
o A class declare with final modifier can not be inherited (We can’t create its subclass).
o When we may like to prevent the class being further subclassed for security reasons.
o Final can be used to even disallow the inheritance, to do this a class can be defined with final modifier.

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 8 of 10


o Declaring a class as final, declares all its method as final.
o It prevents from the unwanted extensions of the class.
o Syntax : final class ClassName { ….. Defination ……. }
USE OF SUPER KEYWORD
 Super is a referance variable that is used to refer immediate parent class object. Uses of uper keyword are as
follow
o Super() is used to invoke immediate parent class constructors.
o Super is used to invoke immediate parent class method.
o Super is used to refer immediate parent class variables.

ABSTRACT METHODS & CLASSES.


 When the keyword abstract appears in a class defination, it means that zero or more of its methods are
abstract.
 There are situations in which you want to define a superclass that declares the structure of a given abstration
without providing a complete implementation of every method.
 This is exactly opposite case of final.
 Abstract Methods
o The abstract keyword is use to declare the method as abstract.
o An abstract method consists of method signature but no method body.
o The subclasses either override the abstract method, or declare itself as abstract.
o The method declare with abstract modifier must be override in sub class.
o The meaning of abstract method is given by the sub class.
o The Constructor or static methods are never declare as abstract.
 Abstract Class
o These classes are used as Super Class.
o Object cannot be created of abstract class.
o Abstract classes basically provide a guideline for the properties and methods of an object.
o In order to use abstract class, they have to be subclassed.
o When a class contain one or more methods as abstract, the class must be declare as abstract.
o The abstract class is not instantiated because it’s some methods are not implemented.

STATIC MEMBERS AND METHODS.


 Static is a keyword.
 It is used to declare data member or methods of class, as a static.
 Static data members and static methods are in the scope of class.
 Static data members are referred as class variables.
 Static methods are referred as class methods.

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 9 of 10


 Static members are accessed by classname directly rather than object.
 Only one copy of static variables is created in the scope of class and it is shared by all the objects of class.
 Static variables are also accessed in instance methods of class.
 Static methods can only invoke other static methods of class but not instance method.
 Static methods can access, only static variables of class but not instance variables.
 Static modifier is not used for constructor.
 Static method can be overload.
 Syntax to declare static data member in class:
o static datatype variable;
 Syntax to declare static method in class:
o static returntype methodname(list of arguments) { … Defination ……}
 Example of Static Data member and methods:

Subject: Java Programming (Inheritance) Print Date: 10/Apr/2019 Page 10 of 10

You might also like