Inheritance ppt

7,784 views 15 slides Oct 02, 2018
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

java


Slide Content

INHERITANCE & METHOD OVERRIDING ADVANCED JAVA PROGRAMMING NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE,THENI. B.NIVEGEETHA(I-MSC(CS))

INHERITANCE Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class. Important terminology: Super Class:  The class whose features are inherited is known as super class(or a base class or a parent class). Sub Class:  The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the super class fields and methods. Reusability:  Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

SINGLE INHERITANCE Types of Inheritance in Java Below are the different types of inheritance which is supported by Java. Single Inheritance :  In single inheritance, subclasses inherit the features of one super class . In image below, the class A serves as a base class for the derived class B.

MULTILEVEL INHERITANCE Multilevel Inheritance :  In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members .

HIERARCHICAL INHERITANCE Hierarchical Inheritance :  In Hierarchical Inheritance, one class serves as a super class (base class) for more than one sub class . In below image, the class A serves as a base class for the derived class B,C and D.

MULTIPLE INHERITANCE Multiple Inheritance (Through Interfaces) :  In Multiple inheritance ,one class can have more than one super class and inherit features from all parent classes. Please note that Java does  not  support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and B.

HYBRID INHERITANCE Hybrid Inheritance(Through Interfaces) :  It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

IMPORTANT FACTS Default super class : Except Object class, which has no super class, every class has one and only one direct super class (single inheritance). In the absence of any other explicit super class, every class is implicitly a subclass of Object class. Super class can only be one:  A super class can have any number of subclasses. But a subclass can have only  one  super class. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java. Inheriting Constructors:  A subclass inherits all the members (fields, methods, and nested classes) from its super class. Constructors are not members, so they are not inherited by subclasses, but the constructor of the super class can be invoked from the subclass. Private member inheritance:  A subclass does not inherit the private members of its parent class.

GENERAL SYNTAX OF INHERITANCE public class Class extends ParentClass {   //new variable or methods here }

EXAMPLE public class Shape { int length; int breadth; } public class Rectangle extends Shape { int area; public void calculateArea() { area = length*breadth; } public static void main(String args[]) { Rectangle r = new Rectangle(); //Assigning values to Shape class attributes r.length = 10; r.breadth = 20; //Calculate the area r.calcualteArea(); System.out.println("The Area of rectangle of length \"" +r.length+"\" and breadth \""+r.breadth+"\" is \""+r.area+"\""); } }

METHOD OVERRIDING Declaring a method in  sub class  which is already present in  parent class  is known as method overriding. Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method . 

class Human { //Overridden method public void eat() { System.out.println("Human is eating"); } } class Boy extends Human { //Overriding method public void eat() { System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); //This will call the child class version of eat() obj.eat(); } }

ADVANTAGE OF METHOD OVERRIDING The main advantage of method overriding is that the class can give its own specific implementation to a inherited method  without even modifying the parent class code . This is helpful when a class has several child classes, so if a child class needs to use the parent class method, it can use it and the other classes that want to have different implementation can use overriding feature to make changes without touching the parent class code.

RULES OF METHOD OVERRIDING Argument list: The argument list of overriding method (method of child class) must match the Overridden method(the method of parent class). The data types of the arguments and their sequence should exactly match. 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, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class. Binding of overridden methods happen at runtime which is known as  dynamic binding If a class is extending an  abstract class  or implementing an  interface then it has to override all the abstract methods unless the class itself is a abstract class.

END
Tags