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);
      }
}


Wednesday 14 March 2018

Can we call non-static method from static method in java?



Suppose we are writing a code where we intend to call any non-static method from within a method that is declared static

Since main() method is static so lets try to call any non static method or variable from within main method.

See the code snippet below where duplicateString  is being called within main method. 


Do you think this code snippet will compile and execute normally? 


package String_Programs;

public class DuplicateString {
      
       public void duplicateString(String str){
           System.out.print(str);
       }

       public static void main(String[] args) {
              duplicateString("This is This and That is This");

       }

}

OUTPUT:
Compile time error

In this case at you will get red underline under duplicateString called within main( ) method in eclipse. Bring your cursor on the method name and you will see this message - 'Cannot make a static reference to the non-static method duplicateString(String) from the type DuplicateString'



Calling non-static method from a static method directly is not allowed in java

How to solve the compile time error?

1. Declare the method to be called as static

You need to declare the duplicateString(String str) method static as shown below.
public static void duplicateString(String str)

Lets re-write our code again.



package String_Programs;

public class DuplicateString {
      
       public static void duplicateString(String str){
             
       }

       public static void main(String[] args) {
              duplicateString("This is This and That is This");

       }

}

OUTPUT:
This is This and That is This



2. Create the instance of the class and then call the method

We have shown an example in the below screenshot.


can we call non static method from static method in java by javaradar
















OUTPUT:

Friday 2 February 2018

Abstract class Vs Interface in java

Abstract class and interface differ based on following parameters:


Methods

An abstract class can have both abstract methods and concrete methods.
An interface has all the methods as abstract. Methods in interface is 100% abstract.

Keyword used to declare

To declare abstract class use the keyword ‘abstract’. 
Example abstract class dummyClass{//abstract methods, methods}

To declare interface use the word ‘interface’. 
Example interface dummyInterface{//abstract method (s)}

Method declaration

In abstract class, to declare any method as abstract use of abstract keyword is mandatory.
In interface, to declare methods as abstract use of abstract keyword is optional. Methods in interface is by default abstract.

Inheritance Supported

Being a class, an abstract class cannot inherit multiple classes. It can only inherit one class.
An Interface can extend (or inherit) multiple interfaces.

What can be extended?

An abstract class can extend either normal class or abstract class.
Interface can extend only interface but not a class.

Variable type & Access modifier

An abstract class can have static, non-static, final or non-final variable with any access modifier.
An interface can only have public static final variable. In interface variable is constant.

Saturday 13 January 2018

What are classes implementing Set interface?

We have three classes implementing set interface in java Collection framework hierarchy. Look at the diagram given below:

set interface java radar


From the diagram its clear that classes implementing set interface are:

1. Hashset

HashSet implements Set interface and extends AbstractSet class. It is part of java.util package.
HashSet contain unique elements only. If you add duplicate in it, previous value will be overwritten.
HashSet allows one null value only.
HashSet doesn’t maintain insertion order so when you retrieve elements from it they may be returned in random order. If you want to get element in order of insertion, use LinkedHashSet.

For more details about Treeset follow link Hashset in java


2. LinkedHashset

LinkedHashSet implements Set interface and extends HashSet class. It is part of java.util package.

LinkedHashSet is almost similar to HashSet except that it maintains the insertion order of the elements.

For more details about Treeset follow link LinkedHashset in java


3. Treeset

Treeset class implements set interface and extends AbstractSet class.
When storing element in treeset, it stores them in ascending order by default.

For more details about Treeset follow link Treeset in java

Tuesday 9 January 2018

What are the classes implementing List interface?

We have three classes implementing list interface in java Collection framework hierarchy. Look at the diagram given below:

list interface java radar



From the diagram its clear that classes implementing list interface are:


1. ArrayList
  • Arraylist was added in Java 1.2 version.
  • Arraylist uses grow able array and thus can grow automatically.
  • Arraylist in java is not synchronized or thread safe.
  • As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.
  • Arraylist's  iterator and list iterator are fail fast in nature.
  • Follow the link to read in detail about arraylist

Read: Why iterator on arraylist is fail fast in java?


2. LinkedList
  • Linkedlist was added in Java 1.2 version
  • Linkedlist in java is not synchronized
  • Linkedlist stores element in nodes which has capability to store element and its address.
  • Follow the link to read in detail about linkedlist

Read: Difference Arraylist vs Linkedlist


3. Vector
  • Vector is synchronized in nature. This is the major difference between array list and vector.
  • Vector is a legacy class and is not recommended to be used at present.
  • Follow the link to read in detail about vector


You may also like to read about:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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.