How to open a program like notepad, pdf file using Java?

Java program to open any program or file.
Example(s) –
1. Open notepad program using java.
2. Open a pdf file using java.

This example will open a notepad for you.


This is a sample example to open a pdf file using java.

To do so you just need to write some java programming.

Here is the example:



The java.lang.Process.waitFor() method causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the sub-process has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the sub-process exits.

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. Click to Know more…

multiple inheritance using interface

Interface in Java

This post explains Interface in java.

What is Interface?
Interface is a blue print of class. Unlike class it contains only public & abstract methods and public, static & final fields.

Interface Syntax:


Difference between Interface and Class
1. Interface: Have only abstract methods
Class: Cannot have abstract method

2. Interface: Multiple Inheritance possible
Class: Multiple Inheritance not possible

3. Interface: Can not be instantiated
Class: Can be instantiated

4. Interface: Do not have constructors
Class: Have constructor

Some facts about Interface
1. Interface is use to achieve loose coupling
See Example

2. Multiple inheritance can be achieve by using interface
See Example

3. Interface provides full abstraction

4. Interface can extends multiple Interfaces

5. An interface does not contain any constructors, that is why Interface can not be instantiated

—InterfaceClass —

—MainClass —

6. JVM itself writes the public static final before the data members and public abstract before methods in Interface if programmer do not write it

For Example:

After Compilation

loose coupling

Loose coupling example using Interface

Loose coupling example using Interface

Loose coupling has advantage over tight coupling.

Lets understand it with example
In below example we have two classes A and B

Class B implements Interface i.e. InterfaceClass.

InterfaceClass defines a Contract for B class as InterfaceClass have abstract methods of B class that can be access by any other class for example A.

In Class A we have display method which can except object of class which implements InterfaceClass (in our case it is B class). And on that object method of class A is calling display() and getVar() of class B

In MainClass we have created object of Class A and B. And calling display method of A by passing object of B class i.e. objb. Display method of A will be called with object of B class.

Now talking about loose coupling. Suppose in future you have to change the name of Class B to ABC then you do not have to change its name in display method of class B, just make the object of new (ABC class) and pass it to the display method in MailClass. You do not have to change anything in Class A





loose coupling

Multiple Inheritance In Java

Multiple Inheritance in Java has always been a topic of debate among people.

Based on the Oracle’s Java tutorial ( inheritance is a concept reserved to classes only, not Interfaces, as what is inherited is the state and behavior which interfaces don’t have.

So, java does not support multiple inheritance but this questions can be asked in other sense also, like:
If the question was: Can we extend more than one class, the answer is no.
If the question was: Can we inherit state and behavior from different classes, the answer is yes with polymorphism.

There are two methods explained in this post of doing multiple inheritance in java.

1. Using interface (see Method 1).
2. Also there is another method do to it (see Method 2)

Method 1:




multiple inheritance using interface

Method 2:





multiple inheritance

Method Overloading v/s Method Overriding – Java

The capability of more than one objects to respond to the same message in a different way can be broadly categorized into two types:

1. Method Overloading
2. Method Overriding

If we have methods with the same name but having different signatures or behaviors in the same scope than the methods are known as overloaded methods and the process is known as method overloading.

a. A Method can be overloaded in the same class as well as in its sub-class.
b. Constructors can also be overloaded. In this case we call the process as Constructor overloading.

Example-1: Method Overloading –

Method having different return types but not having different signatures cannot be said as overloaded methods because it will result in error stating “signature should be different”.
void display(){
int display(){

e.g. of overloaded methods in System class are print() and println()

Now studying about method overloading in Java rises a question in our minds.
Do you know what is that question?
Read below:
Question – Can We overload main method of Java?
Answer – The simple answer is Yes. This is how we will prove it:


Method overriding means having a different implementation of the same method in the inherited or derived class. These two methods would have the same signature, but different implementation. One of these would exist in the base or the super class and other in the derived class. These methods cannot exist in the same class.

Example-2: Method Overriding –


We use @overrride annotation just before the method to denote an overriden method.