Page 1 of 1

Multiple Inheritance in Java Contrast C++ with examples Rate Topic: ***** 2 Votes

#1 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Posted 19 February 2009 - 07:35 PM

*
POPULAR

Multiple Inheritance in Java

What is multiple inheritance? It is the ability for a class to be derived from more then one class. To fully appreciate what Java can do in this regard we must take

A Quick Detour into C++

C++ allows multiple inheritance easily, depending on how the programmer sets up the base classes, etc... Consider the following example in C++:

#include <iostream>
#include <string>
using namespace std;

//base class
class Animal {
protected: //derived classes get these
	int age;
	string name;
public:
	Animal() {};
	string getName()		{return name;};
	int getAge()			{return age;};	
};

//Bird is a derived class from Animal
class Bird : public virtual Animal {
public:
	void birdNoise()		{ cout << "Bird Noise!\n";};
	Bird() {};
};

//Horse also derives from Animal
class Horse : public virtual Animal {
public:
	void horseNoise()		{ cout << "Horse Noise!\n";};
	Horse() {};
};

class Pegasus : public Bird, public Horse {
	//reserved for future items 
public:
	Pegasus(string n, int a) {name = n; age = a;};
};

int main()
{
	Pegasus* peggi = new Pegasus("Pegasus", 5); //create the pegasus object
	cout << peggi->getName() << "'s age is " << peggi->getAge() << endl;
	peggi->birdNoise();
	peggi->horseNoise();
	delete peggi;
	return 0;
}



A possible UML diagram based on the following:

Attached Image

We set up all of that infrastructure to allow Pegasus to be both a bird and a horse, which is important because he's a flying horse. While on a small scale this seems unnecessary and overcomplicated, as a project grows in both size and scope, determining how objects relate to each other will become more apparent.

But wait, you say, this is the Java tutorial section...you are absolutely right, let's not digress:

Back to Java

We can't do the above directly with Java (indirectly we can though), as it only supports single direct inheritance, such as:

public class Animal {

//animal stuff

}

public class Bird extends Animal {

//Bird stuff
}

//However we can't have another class extend Bird vis a vis extending Animal as well



What we can do is use an interface to simulate multiple inheritance:

//Base class
public class Animal {

	protected int age;
	protected String name;

	public Animal() 			{}
	
	public String getName()		{return name;}
	public int getAge()			{return age;}			
}



Now, instead of having Bird and Horse classes extend Animal (single inheritance), we'll make them interfaces. Any class that implements an interface is required to override the methods presented in them. This is for a programmer's own protection:

//****************SYNTAX***********************
public interface interfaceName {
	 void abstractMethod();
	  int overrideMe();
	//etc...
}
//**********************************************
public interface Bird {
	//all implementers have to overwrite methods
	void birdNoise(); 
}

public interface Horse {
	//all implementers have to overwrite methods
	void horseNoise(); 
}



The java syntax for inheritance and interfaces:

public class className extends superClass implements interfaceOne, interfaceTwo, ...



Here is the Pegasus class (from above, converted to Java) with a main() example:

import java.io.*;
					 //single inheritance, multiple interfaces
public class Pegasus extends Animal implements Bird, Horse {
	public Pegasus(String n, int a) 
	{
		name = n; 
		age = a;
	}
	
	//overriding interface methods
	public void horseNoise()
	{
		System.out.println("Horse Noise!");
	}
	
	public void birdNoise()
	{
		System.out.println("Bird Noise!");
	}
	
	
	public static void main(String[] args)
	{
		Pegasus peggi = new Pegasus("Pegasus", 5);
		System.out.println(peggi.getName() + "'s age is " + peggi.getAge());
		peggi.birdNoise();
		peggi.horseNoise();
	}
}



A possible UML diagram based on the java implementation:

Attached Image

A visual is often the best learning aid. Quick synopsis: Based on interfaces we can "fake" multiple inheritance in java since only direct single inheritance is supported. We therefore extend to the base class we plan on using and then use interfaces to allow the object to have access to as many functions as we want. Remember, we must write each class's own implementation for each method in the interface. In large projects, using interfaces allows us to control the flow of code and data access, which is crucial. Hopefully you found this tutorial helpful. You are free use the code/images in any manner that you see fit. Happy coding!

--KYA

Is This A Good Question/Topic? 7
  • +

Replies To: Multiple Inheritance in Java

#2 programstudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 10

Posted 04 February 2010 - 04:24 AM

hi,
Good Explanation very much helpful
In the uml diagram for java there is no connection to Animal from Bird and horse why?
Is it necessary to use the implement the same method in the derived class and why
[code]void birdNoise();
[code] void horseNoise();
why in the Peagus class
[code]public void horseNoise()
[code] {
[code] System.out.println("Horse Noise!");
[code] }

[code] public void birdNoise()
[code] {
[code]System.out.println("Bird Noise!");
[code] }
why this must be there

Why "Remember, we must write each class's own implementation for each method in the interface. " reason?
Thank for this good explanation
Thank you

This post has been edited by programstudent: 04 February 2010 - 04:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 programstudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 10

Posted 04 February 2010 - 04:38 AM

hi,
Please expalne the same with a java project
For example a login program for example in swing
forget the part of gui creation just explain the concept of inheritence in login section means the base class for the database connection -class animal,class bird -count the login,class horse-check valid user, class peagus accepting the value from the gui

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1