Q:

Overriding method with a lower-visibility one results in compilation error

I have one class and one interface:
public interface A {
    public void getNum();
}

public class B {
    public void getNum() {
        System.out.println("4");
    }
}

public class C extends B implements A {
    protected void getNum() {
        System.out.println("3");
    }
}

Now my question is, why this code is giving compilation error and how can we avoid it. Is there any way in which we can override this method in class C?

A:

From Java Language Specification:
jls-8.4.8.3

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:

If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
...

Notice that you are trying to override public method getNum() inherited from class B (and also from interface A) with new one that has protected access modifier. It means that you are trying to reduce visibility of this method which according to specification is incorrect.
To be able to override this method you need to use public access modifier with your new version of that method.

Why you cant reduce visibility? Take a look at below code which uses your classes but is placed inside some other package and ask yourself "how should this code behave?".
package my.pckage;

import your.pckage.A;
import your.pckage.C;

public class Test{
    public static void main (String[] args){
        C C = new C();
        c.getNum();// ERROR: Test class doesn't have access to `c`s protected method.
                   // Why should it have, Test doesn't extend C.

        A a = (A)c;// Lets try using other reference
        a.getNum();// Should `a` have access to method that is protected in `C`?
                   // If yes, then what is the point of declaring this method 
                   // protected if all I would have to do to get access to it is 
                   // casting instance of C to A interface?
    }
}

A:

Fix the typos and try again ;)
public interface A {
    public void getNum();
}

public class B {
    protected void getNum() {
        System.out.println("4");
    }
}

public class C extends B implements A {
    public void getNum() {
        System.out.println("3");
    }
}

A:

First of all scope should be from lower to higher while you are overriding method in Java. scope of subclass method should be high then super class for e.g
Valid Overriding 
class B {
    protected void getNum() {
        System.out.println("4");
    }

 class C extends B  {
    public void getNum() {
        System.out.println("3");
    }

InValid Overriding 
class B {
    public void getNum() {
        System.out.println("4");
    }

class C extends B  {
    protected void getNum() {
        System.out.println("3");
    }

Your second problem is you have created two public class which is not valid you can create only one public class in your java file.

