December 30, 2017

Inheritance

Inheritance:

Inheritance is the process of acquiring all the properties and behaviors of parent object.
When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields to the new class.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
In general, we call Parent class as Super class and the child class as Sub Class

Syntax of Java Inheritance

class Sub_Class extends Super_Class 
//methods and fields
 The extends keyword indicates that we are creating new class(childClass) that derives from an existing class(parentClass). The meaning of "extends" is to increase the functionality.
Ex:
class Phone{
            int memory=4;
            float screenSize=5.5; 
class iPhone extends Phone{ 
            String manufacturer = "Apple";
            public static void main(String args[]){ 
                        iPhone ip=new iPhone(); 
                        System.out.println("iPhone Memeory is: "+ip.memory);
                        System.out.println("iPhone Screen size is: "+ip.screenSize);
                        System.out.println("iPhone Manufacturer is: "+ip.manufacturer);
            }
}

 Pictorial Representation:

In the above the diagram, iPhone is a subclass of a super Class Phone. Which means iPhone Is A type of Phone.
It means here we created IS-A relation between super class and sub class using inheritance.

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Note: Multiple Inheritance is not supported in java through class.

Single Inheritance:

In single inheritance, subclasses inherit the features of one superclass. In program below, the class Phone serves as a base class for the derived iPhone
            int memory=4;
            float screenSize=5.5; 
class iPhone extends Phone{ 
            String manufacturer = "Apple";
            public static void main(String args[]){ 
                        iPhone ip=new iPhone(); 
                        System.out.println("iPhone Memeory is: "+ip.memory);
                        System.out.println("iPhone Screen size is: "+ip.screenSize);
                        System.out.println("iPhone Manufacturer is: "+ip.manufacturer);
            }
}

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 Program image, the class Phone serves as a base class for the derived class SmartPhone, which in turn serves as a base class for the derived class iPhone. In Java, a class cannot directly access the grandparent’s members.
class Phone{
            void call();
            void message();
class SmartPhone extends Phone{ 
            void toggleWifi();
            void toggleBluetooth();
}
class iPhone extends SmartPhone{
            void connectToMAC();
            void siri();
}

Hierarchical Inheritance:

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class. In below image, the class Phone serves as a base class for the derived class iPhone and AnroidPhone
class Phone{
            void call();
            void message();
class SmartPhone extends Phone{ 
            void call();
void message();
void toggleWifi();
            void toggleBluetooth();
}
class iPhone extends SmartPhone{
            void connectToMAC();
            void siri();
}
class AnroidPhone extends SmartPhone{
void connectToPC();
}

Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where Java, Test and JavaTest are three classes. The JavaTest class inherits Java and Test classes. If Java and Test classes have same method and you call it from child class object, there will be ambiguity to call method of Java or Test class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
class Java{
void print(){System.out.println("Java");} 
class Test{ 
void print(){System.out.println("Test");} 
class JavaTest extends Java,Test{       //suppose if it were
public Static void main(String args[]){
JavaTest obj=new JavaTest ();
obj.print();      //Now which print() method would be invoked?

0 comments:

Post a Comment