Let start with two small example. We are having two classes, i.e.: Class RetroCar and Class LuxCar. So whatever inside the Class RetroCar will be inherited into LuxCar.
public class RetroCar {
// the RetroCar class has three fields
public int cadence;
public int gear;
public int speed;
// the RetroCar class has one constructor
public RetroCar(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the RetroCar class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
The above is RetroCar.java code. You have to save the file as RetroCar.java in the same project.
Now Let’s have a look different class how we can inherit the RetroCar.
public class LuxCar extends Bicycle {
// the LuxCar subclass adds one field
public int seatHeight;
// the LuxCar subclass has one constructor
public LuxCar(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the LuxCar subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
Remember when inheritance is happen, we are excluding the constructor which are in the Base Class. In this example, our Base Class is the Class RetroClass. And the LuxCar is our derived class. If we even want to access the constructor, then we must use class and object concept rather than the inheritance. In inheritance concept, we just don’t call the constructor method as they are in class and object concept.
Let’s have a look on how the Inheritance can be implemented in Java Language:
There are five types of Inheritance:
class A {
int x;
int y;
int get(int p, int q){
x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}
class B extends A{
public static void main(String args[]){
A a = new A();
a.get(6,8);
a.Show();
}
void display(){
System.out.println("B");
}
}
class A {
int x;
int y;
int get(int p, int q){
x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}
class B extends A{
void Showb(){
System.out.println("B");
}
}
class C extends B{
void display(){
System.out.println("C");
}
public static void main(String args[]){
A a = new A();
a.get(5,6);
a.Show();
}
}
The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.
In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.
super keyword
The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.
The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.
e.g. Suppose class A is the super class that has two instance variables as int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.
super.member;
Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of a super class having the same name. The following example clarify all the confusions.
class A{
int a;
float b;
void Show(){
System.out.println("b in super class: " + b);
}
}
class B extends A{
int a;
float b;
B( int p, float q){
a = p;
super.b = q;
}
void Show(){
super.Show();
System.out.println("b in super class: " + super.b);
System.out.println("a in sub class: " + a);
}
public static void main(String[] args){
B subobj = new B(1, 5);
subobj.Show();
}
}
Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command.
super(param-list);
Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor.
So to implement multi-inheritance, we are going to make interface(s) and inherit those.
interface xyz{
//Interface xyz
public static final int i = 9;
public abstract void test();
}
interface abc{
//Interface abc
public static final int j = 5;
public abstract void example();
}
class ZoSoft implements abc,xyz{
//Here we have two interface with this class
//So re-define the Methods from those interface
public void example(){
System.out.print("Some Example goes Here!");
}
}
Each class should be keep in a separate java class file.
Source : http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html and http://www.roseindia.net/java/language/inheritance.shtml
Leave a Reply
You must be logged in to post a comment.