Sunday, 22 April 2018

Why Java doesnot support multiple Inheritance?

When a class can extend more than one class then this capability is known as "multiple inheritance."

But java doesn't support multiple inheritance.
We are going to understand the reason behind java not supporting multiple inheritance.


Reason
The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.
The problem is that if a class extended two other classes, and both super classes had, say, a doTask() method, which version of doTask() would the subclass inherit?
Ambiguity arises at this point.

This issue can lead to a scenario known as the "Deadly Diamond of Death," because of the shape of the class diagram that can be created in a multiple inheritance design.

The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond.

Lets understand the same with the help of a diagram :
           
Why multiple inheritance not supported in java?

Wednesday, 18 April 2018

Java Project - Number System conversion

Here we are trying to create a java project where you will first let user choose what conversion they want to perform.
Allow user to enter value in the required format and then perform the conversion. On the completion of the process print the converted number format.


Note: The project is in progress. If you want to create on your own you can use the template and write code to perform other conversions as well that is missing here.


Controller.java


package NumberSystem;

import java.util.Scanner;

public class Controller {

      public static void main(String[] args) {
            System.out.println("Enter your choice");
            System.out.println("1: To convert decimal to binary ");
            System.out.println("2: To convert binary to decimal");
            System.out.println("3: To convert ........");  // More to be added soon
           
            Scanner scNum = new Scanner(System.in);
            int choice = scNum.nextInt();
            //System.out.println("You enetered: "+ i);
           
            switch(choice){
            case 1:
                  System.out.println("Enter decimal Number");
                  Scanner sc = new Scanner(System.in);
                  int decNum = sc.nextInt();
                  DecimalToBinary dtb = new DecimalToBinary();
                  dtb.decToBinary(decNum);
                  break;
            case 2:
                  System.out.println("Enter decimal Number");
                  Scanner sc1 = new Scanner(System.in);
                  int binNum = sc1.nextInt();
                  BinaryToDecimal binDec = new BinaryToDecimal();
                  binDec.binToDec(binNum);
                  break;
            case 3:
                  break;
            default: System.out.println("Wrong choice");
            }

      }

}


DecimalToBinary.java


package NumberSystem;

public class DecimalToBinary {

      public void decToBinary(int dec){
            if(dec==0){
                  System.out.println(0);
                  return;
            }
            String binaryString = "";
            while(dec>0){
                  int rem = dec%2;
                  binaryString = rem + binaryString;
                  dec = dec/2;
            }
            System.out.println(binaryString);
      }
}


BinaryToDecimal.java


package NumberSystem;

public class BinaryToDecimal {

      public void binToDec(int binNum){
            int decimalForm = 0;
            int power = 0;
           
            if(binNum==0){
                  System.out.println(0);
                  return;
            }
            while(binNum>0){
                  int tmp = binNum%10;
                  decimalForm += tmp*Math.pow(2, power);
                  binNum = binNum/10;
                  power++;
            }
            System.out.println(decimalForm);
      }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.