Inheritance in Java
2 posters
Page 1 of 1
Inheritance in Java
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 :
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:
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.
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
Re: Inheritance in Java
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.
Similar topics
» Java Tutorial: Using Two Java Reference Keywords, this and super
» About Java 3D
» Java Swing Example
» PDF Generation in Java
» Java 7
» About Java 3D
» Java Swing Example
» PDF Generation in Java
» Java 7
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford