Friday 5 June 2015

Inheritance in Java

Inheritance:
extends is keyword is used

multiple inheritance is not allowed in java:
class A extends B, C // this is not allowed

multilevel inheritance is possible via interfaces

constructors are never inherited. Everything else is.

consider First obj=new First();
on printing obj, the addreess of obj will be printed. or rather the address contained in obj [its value as it is nothing but a reference variable i.e. something containing an address]

class A
{
int a;
}

class B extends A
{
int a;
}

here a means B's a
and super.a means A's a.
the same concept can be used to reference method of inherited classes with the same names as methods in the parent class.
the super keyword is optional for use only when there is duplicated names.

super() has to be the very first statment in the child 'class' constructor.

only child class can use the super keyword.


No comments:

Post a Comment