Search This Blog

Monday 15 September 2014

Why Multiple Inheritance is not supported in Java.?

Inheritance:  When an object or class is based on another object or class, using the same implementation (inheriting from a class) or specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy.


1:  class SuperClass{  
2:    void superClassMethod(){  
3:      //Code  
4:    }  
5:  }  
6:  class SubClass extends SuperClass{  
7:    void subClassMethod(){  
8:      //Code  
9:    }  
10:  }  

In the above code, SubClass extends SuperClass and inherits the properties of SuperClass.

A class basically has following:
1. State
2. Field

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). 
Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Now suppose you are able to inherit more than one class in Java.
Classes-
1. Animal
2. Mammal
3. Bird
4. Dog
Mammal and Bird inherited Animal class which is possible in Java. A class can be inherited by any number of classes if it allowed to be inherited. Dog class is actually a animal (is-a relationship). 

Dog can extend only one class according to Java Language specifications, which means a Dog can acquire the properties of only one class directly. 
Now suppose if your class Dog is capable of inheriting two classes- Mammal and Bird at a time. And when you instantiate a Dog class, the object of Dog will inherit the state and behavior of all the extended superclasses.  Just imagine this example in real world, what if you see a Dog who is flying in above you?

This problem of inheriting properties of more than one class at a time is also known as Diamond Problem
As in the above figure on left you can see, Animal class is extended by two classes - Mammal and Bird which is possible. Now the a dog class extending two classes - Mammal and Bird.

Method and constructor of superclasses accessing/defining the same field as method and constructor of subclasses. Which method or constructor will take precedence? We can't decide the precedence.
This is the reason why multiple inheritance is not there in Java.

There are people out there who say- Interfaces are meant to achieve multiple inheritance. Interfaces is not actually meant for achieving multiple inheritance.
But yes, we can simulate the behaviour of Multiple Inheritance using interface.
Because interfaces do not contain fields and because of this you do not have to worry about problems that result from multiple inheritance of state.

Friday 31 January 2014

Changing the TextColor of Console in Code::Blocks

Hello Programmers,
In this post you will learn about changing the Color of Text on Console using Code::Blocks IDE and MinGW Compiler.
Here we are using a function sytem() which is defined in stdlib.h header file.
The function system() can be used in following two ways:
  • Passing a color code just to change the foreground of the text.
    Syntax:           system("COLOR <color code>");
    Example:        system("COLOR 4"); 
  • Passing two color codes, one for background and one for foreground of the console.
    Syntax:           system("COLOR <color codeF><color codeB>");
                           color codeF is for foreground and
                           color codeB is for background.
    Example:        system("COLOR AC"); 



  1. /*
  2. Aditya's Console Color Changing Code Example
  3. Compiled On:    MigGW (CODE::BLOCKS)
  4. This code will change the Foreground color of the console text.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. int main()
  9. {
  10.     system("COLOR 6");
  11.     printf("Welcome to the COLOUR changing Console Application!\n");
  12.     return 0;
  13. }

Using system("COLOR 6");


       /*Code to change Foreground and Background of console.*/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {

  5. /* 
    Along with COLOR you can pass two codes 

    Here I have used F for Foreground and C for Background


    */ 
  6.     system("COLOR FC");
  7.     printf("Welcome to the COLOUR changing Console Application! \nThis code changes ForeGround and BackGround");
  8.     return 0;
  9. }


    Using system("COLOR FC")
Once you have understood the concept of using system("COLOR <color code>"); try to play with the given color codes.


/*
Color Codes:
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
*/