Thursday, June 22, 2017

Java-Inheritance

inheritance
-----------
Deriving properties of a super class to a sub class is known as inheritance.

It avoids repitition of code.

save memory.

Maintainence of project becomes easy.

code readability.

code resuability.

we can have proper code modularization with inheritance.

ex:
         Flower
         name
         color
         price
|          |         |
Rose     Jasmine   Lotus.

All the common properties of sub classes are declared in a class called as super class.

A super class is top class level class.

A sub class is a bottom level class.

we can extend(inherit) one class from the other using "extends" keyword.

A private member of a super is not inherited to a sub class.

Types of inheritance
--------------------
we have 5 types of inheritance

i)single inheritance
ii)Multi level inheritance
iii)heirarchical inheritance
iv)Multiple inheritance
v)Hybrid inheritance

single inheritance
------------------
 A

 ^
 |

 B          

Deriving one subclass from one super class is known as single inheritance.

Multi level inheritance
-----------------------
A


^
|

B

^
|

C
.
.
.
.

Deriving a sub class from a super class and in turn deriving another sub class from the derived class and so on is known as multi level inheritance.

Program
-------
class A
{
 int a;
 int b;
 
 public void init()
 {
   a=10;
   b=20;
 }
 public void display()
 {
    System.out.println(a);
    System.out.println(b);
  }
}
class  B extends A
{
    int c;
    public void sum()
    {
       c=a+b;
       System.out.println(c);
    }
}
class  C extends B
{
    int d;
    public void mul()
    {
       d=a*b*c;
       System.out.println(d);
    }
    public static void main(String args[])
    {
        C c=new C();
        c.init();
        c.display();
        c.sum();
        c.mul();
       
    }
   
}

   
In a multi level inheritance, a sub class can access the properties of all its super classes.

Whenever we create an object for a sub class,a sub class object internally consists of a super class object as shown below.



Heirachical inheritance
-----------------------
Deriving 2 or more subclasses from a super class is known as heirarchical inheritance.

     A
^    ^  ^
|    |  |
B    c  D

Program
-------
class A
{
 int a;
 int b;
 
 public void init()
 {
   a=10;
   b=20;
 }
 public void display()
 {
    System.out.println(a);
    System.out.println(b);
  }
}
class  B extends A
{
    int c;
    public void sum()
    {
       c=a+b;
       System.out.println(c);
    }
}
class  C extends A
{
    int d;
    public void mul()
    {
       d=a*b;
       System.out.println(d);
    }
    public static void main(String args[])
    {
        B b=new B();
        b.init();
        b.sum();

        C c=new C();
        c.init();
        c.mul();


       
    }
   
}

In heirarchical inheritance we inherit properties from a super class to sub class,but its not possible to inherit properties from subclass to subclass.

Multiple inheritance
--------------------
Deriving a sub class from 2 or more super classes is known as multiple inheritance.

In the above code




Hybrid
------
Combining any 2 inheritances is known as hybrid inheritance.



single inheritance
------------------

syntax
------
      accessmodifier class SuperClassName
      {
        //statements
      }
accessmodifier class SubClassName extends                                              SuperClassName
      {
        //statements
      }

Program
-------
class A
{
    int a;
    int b;
    public void input()
    {
      a=10;
      b=20;
    }
    public void display()
    {
      System.out.println(a);
      System.out.println(b);
    }
}
class B extends A
{
int c;
public void sum()
{
   c=a+b;
   System.out.println(c);
}
public static void main(String args[])
{
   B b=new B();
   b.input();
   b.display();
   b.sum();
 
}

}

In the above we are deriving one sub class(B) from one super class(A).

In inheritance it is recommended to create object for a sub class because a sub class can access properties of both sub class as well as super class.


In heritance we can inherit properties of super class to sub class but reverse is not possible i,e we cannot inherit properties of sub class to super class.


Internal representation of objects
----------------------------------
Whenever we create an object for a sub class,a sub class object internally consists of a  super class object as shown in diagram.

How many objects are created?

public static void main(String args[])
{
   A a=new A();
   B b=new B();
 
}

i)1
ii)2
iii)3
iv)No objects are created


Note:
if we declare a static block both in a super and sub class,they are executed from super class to sub class as soon as a .class file is loaded.


In java for each class one .class file is created,
if we have 10 classes then 10 .class files are created.

   No of classes=No of .class file

In a java file we can declare only one public class,if we declare more than one public class we get CE.
example
-------
what is the output of the following code?

public class A
{
}
public class B
{
}

i)compiles but not execute
ii)complies and execute
iii)CE
iv)RE

Analysis
--------
We must declare only one public class in a file.


If we declare a class as public in a file then we must save that file using classname that is declared as public.

example
-------
public class A
{
}
class B
{
}

save as
A.java

Multi level inheritance
-----------------------

syntax
------
accessmodifier class SuperClassName
{

}
accessmodifier class SubClassName1 extends SuperClassName
{

}
accessmodifier class SubClassName2 extends SubClassName1
{

}
.
.
.
.


Deriving super class to sub class is called-------

--- types of inheritances.

----inheritance is not supported in java.

It is recommended to create object for------.

A sub class objects internally consists ------.

-----no .class files are create in a program.

we can declare only one --------class in a file.

If we declare a class as public then we should save that program with-------.

In inheritance , we can derive properties from super to subclass and viceversa
true
false.

main method
-----------
public static void main(String args[])

public--A public method can be accessed by any         program.

static--A static method is a methid which can be         accessed using ClassName without creating         object.

void--doesn't return any value.

main--Execution of a java program begins from main       method,infact it inidcates starting point of       execution to jvm.

String args[]--command line arguments,used to accept                values from command prompt.

Note:
    We can compile a java program without main but we cannot execute a java program without main.


Upto 1.6 version  it is possible to partially execute a java program using static blocks but from 1.7 version onwards we must declared main method to execute a java program.

We can declare multiple main methods in a java program but not in the same class, but in different classes.

Program
-------
class X
{
    public static void main(String args[])
    {
      System.out.println("X");
    }

}
class Y extends X
{
    public static void main(String args[])
    {
      System.out.println("Y");
    }

}
class Test extends Y
{
    public static void main(String args[])
    {
      System.out.println("Test");
    }

}

save as XYZ.java


we can save a java program with any name,if we dont have any public class in a file.

While executing we should run the program by using a class that is having main method inside it.


How to compile
--------------
javac XYZ.java


How to execute
--------------
java X
X
or
java Y
Y
or
java Test
Test

In a program we can declare atmost one public class,if we declare more than one class as public it leads to compilation error.

ex:
public  class A
{

}c
class B
{

}

If we declare a class a public in a file then we must save our file with the name class that is declared as public

ex:
public class A
{

}

save as
A.java.

Every class in java is inherited from a super class called as Object class either directly or indirectly.

Object class is available in the package java.lang.*;

example
-------
Object
 ^
 |
 A
 |
 B
 |
 C
 .
 .
 .

No comments: