Inheritance in Java

View previous topic View next topic Go down

Inheritance in Java

Post by BIT0112-Rokon on Mon May 24, 2010 4:01 pm

What is inheritance in Java?

As the name is inheritance, it means to inherite something from another. In java, inheritance is a compile time* machanism that allows an object to inherit characteristics from another objects.

java allows two types of inheritance. They are -

Class inheritance: create a new class extnesion of another class.

Interface inheritance: create a new class to implement the methods defined in an interface.

Note: For class inheritance java use extends keywords and for interface inheritance java use implements keywords.

What is the subclass and the superclass?

Superclass is the class that is being inherited from and subclass is the class that is inheriting from the superclass. Superclass is a more generic version of class and subclass is a more specialized version of superclass.

Note: As many as possible subclass, a superclass can have, but a subclass can only extend only one superclass.

***Any protected members (fields or methods) of superclass will be vissible in subclass whether it same package or different package.

Limitation of java iheritance :

  • private member of superclass are can not accessed in subclass.

  • Members that have default accessibility in the superclass are also not inherited by subclasses in other packages.

  • Constructor and intilizer block are aslo not inherited in subclasses.

  • Java does not provide multiple inheritance.


  • What is the benefits of Inheritance:


The main benefit of iheritance is code reuse. It minimize the amount of duplicate code in the cases of sharing common code amongst subclasses.

Java inheritance allows more flexibility in the cases of changing things.

Example:

Code:


class FirstName {
    String firstName = "Bazlur";
}
 class LastName extends FirstName {
    String lastName = "Rahman";
    public static void main(String[] args) {
        LastName fn = new LastName();
        System.out.println(fn.firstName + " " + fn.lastName);
    }
}

Compile time: compile time is the time in which you, the programmer are compileing the code.
Run time: the time period in which a user is running the program.


Last edited by bit0112-rokon on Tue Jul 27, 2010 8:28 am; edited 1 time in total

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Inheritance in Java

Post by BIT0104-ANIK on Mon May 31, 2010 8:55 am

this is actually written by you, why you would have to give the link to another website which is actually yours? it's you who should be glorified, not your site.

_________________________________________________________________
Buddhong shoronogocchami..
dhommong shoronogocchami..
shonghong shoronogocchami..
Jogoter sokol prani.. shukhi hok.
avatar
BIT0104-ANIK
Administrator
Administrator

Course(s) :
  • BIT

Blood Group : O+
Posts : 423
Points : 699

View user profile http://anikstech.blogspot.com

Back to top Go down

Re: Inheritance in Java

Post by BIT0112-Rokon on Tue Jul 27, 2010 8:30 am

yap.. link is deleted ..

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Inheritance in Java

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum