Category Archive Java


XOR operator in Java

In the Java programming language, a ‘variable’ is used to store a value. Different kinds of operations can be performed on the variables by means of ‘operators’.  The Java programming language has a number of operators and these are listed below:

Read More


Object oriented programming

‘Object oriented programming’ is a type of programming which involves object oriented principles like encapsulation, polymorphism and inheritance. These principles are implemented by means of classes, methods, variables and other constructs in Java. We saw a sneak peek into object oriented programming in the Java 101 post. 

Read More


Java 101

‘Java’ the object oriented programming language was developed by James Gosling of Sun Microsystems(now a part of Oracle) in 1995. It is a platform independent programming language meaning that once a Java program is written and compiled – the compiled program can run on any architecture(Apple, Windows etc)It is this feature that makes it flexible. It is also much easier to work with Java than other programming languages like C++.

Read More

ByBala Manikandan

Overloading and Overriding Methods in Java

Two terms commonly associated with methods in Java are overloading and overriding. These two concepts will be discussed in the following sections.

Method Overloading

Method overloading is the process of defining more than one method having the same name in the same class (or in the same inheritance tree).

Rules to define overloaded methods:

  • The methods must have the same name.
  • The methods must have different argument lists.
  • They may have same or different return types.
  • Their access levels may be same or different.

The correct method to be executed by the system is decided by the Java compiler at compile time, and this is called static polymorphism. The following example demonstrates method overloading:

public class Test {

       public static int add(int a, int b){    //adds 2 numbers

        return a + b;


       public static String add(String a, String b){  //concatenates 2 Strings with a space in between

        return a + ” ” + b;


       public static void main(String[] args){

           System.out.println(“4 + 5 = ” + add(4, 5));

        //calls 1st method

           System.out.println(“Method + Overloading = ” + add(“Method”, “Overloading”));

        //calls 2nd method



The output of the above program is:

4 + 5 = 9

Method + Overloading = Method Overloading

Method Overriding

Method overriding means giving a new definition to an existing method in a class, in one of its subclasses. This is done to redefine the behaviour of objects of the subclass.

Rules to override a method:

  • The overriding method should be present in the subclass of the class in which the overridden method is present.
  • The overriding and overridden methods should have the same name and argument list.
  • The two methods should have the same return type. Or the return type of the overriding method should be a subclass of that of the overridden method.
  • The access modifier of the overriding method must be either the same as or less restrictive than that of the overridden method.

The method to be executed is decided at runtime (not at compile time), and this is called dynamic polymorphism. The following example demonstrates method overriding:

class A {

    public void display(){

        System.out.println(“Executing from class A”);



class B extends A {

    public void display(){               //override the method display()

        System.out.println(“Executing from class B”);




public class Test1 {

    public static void main(String[] args) {

        A objA = new A();

        A objB = new B();              //an A reference, but a B object







The above code prints:

Executing from class A

Executing from class B

We have seen the core concepts of ‘Overloading and Overriding’ in Java in this post… Join me as I uncover more Java concepts in subsequent posts…

ByBala Manikandan

Java 8 – Default and Static Methods in Interfaces

Before beginning this post, I would like state that this post assumes some knowledge of Java.


Prior to Java 8, all methods in an interface had to be ‘abstract’. However, in Java 8, default and static methods could also be defined in interfaces. These are discussed in the following sections.

Default Methods:

A default method in an interface is used to define the ‘default’ behaviour of an object of that interface type, in case a class implementing that interface does not override the method. Unlike other interface methods, default methods have a method body. A default method is declared using the keyword ‘default’:

interface TestInterface {
      default void defaultMethod() {  }      //a default method

 Flat 20% cashback on Motorola, Vivo and Lenovo Smartphones

Note that the above method uses curly braces, not a semicolon. Just like normal methods, statements can be included between the curly braces. Now consider another piece of code:

package bala;

interface TestInterface{
default void print(){
class Sample1 implements TestInterface{ //overrides print()

public void print(){
System.out.println(“Not Default”);
class Sample2 implements TestInterface{} //doesn’t override

public class Test {

public static void main(String[] args) {
TestInterface obj1 = new Sample1();
TestInterface obj2 = new Sample2();



The above code prints:

Not Default

In the above code, both the classes, Sample1 and Sample2, implement the interface TestInterface, which contains a default method.

class Sample1 overrides the print() method but Sample2 doesn’t.  In the main() method, two objects of object types Sample1 and Sample2 are created, which then invoke the print() method. Since Sample1 has the overridden print() method, the code in the overridden version executes.

But the print() method is not overridden in Sample2,  so the code in the default method of TestInterface executes. This accounts for the above output.

Static Methods:

Recall the definition of static methods – they belong to the class rather to an instance of the class.

As in classes, static methods in interfaces are the methods that can be called using the interface name itself, rather than using an object reference variable. These methods also use curly braces. Their usage is very similar to the usage of static methods in classes, as demonstrated in the following code:

package bala1;

interface TestInterface2{

static void print(){
System.out.println(“Static method inside an interface”);
public class Test2 {

public static void main(String[] args) {



As expected, the above code prints:

Static method inside an interface

We have seen the newer features of Java 8(namely – default and static methods in interfaces) in this post! Join me as I uncover some more technical aspects of the world!


Introduction to Java

We see programming languages all around our digital lives and there have been a few languages that have stood the trials and tribulations of time. Each language is created with a different purpose and sometimes, the whole motive behind it erodes with new technologies.

Read More