Reason Behind 'Could Not Find or Load Main Class' Error in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:19:21

1K+ Views

When write a Java program/class first of all you need to compile it using the javac command as −javac [name of the class].javaIf your program gets compiled without errors, a .class file (byte code) is created with the specified name. Then you need to execute it using the java command (JVM) as −java [class name]ExampleSuppose we Have created a simple class Calculator which adds two or, three numbers in the file with name Calculator.java in the path D:\sample as −public class Calculator {    int addition(int a , int b){       int result = a+b;       ... Read More

Can Super Class Reference Variable Hold Sub Class's Object in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:15:52

3K+ Views

Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).But, using this reference you can access the members of super class only, if you try to access the sub class members a compile time error will be generated.ExampleIn the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.The Student extends the person class and in addition to the inherited ... Read More

Prevent Method Overriding in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:13:44

348 Views

Inheritance can be defined as the process where one (parent/super) class acquires the members (methods and fields) of another (child/sub).If two classes are related to each other with inheritance. If the super class and sub class contains same methods (same name and arguments), When we invoke this it using the sub class object, the method of the sub class will be executed. This mechanism is known as overriding.Overriding final methodsOnce you declare a method final it cannot be overridden If you try to do so, it generates a compile time error −Exampleclass Super{    public final void demo() {     ... Read More

Call a Constructor Directly from a Method in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:12:42

10K+ Views

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class.Therefore, if you need to invoke a constructor explicitly you can do so, using "this()".Invoking a ... Read More

Sequence of Execution of Instance Method, Static Block and Constructor in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:09:31

8K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members of a class. JVM executes static blocks before the main method at the time loading a class.Examplepublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodA constructor is similar to method and it is invoked at the time creating an object of the class, it is ... Read More

Method Overloading by Changing Return Type in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:08:20

1K+ Views

When a class has two or more methods by the same name but different parameters, at the time of calling, based on the parameters passed, respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Exampleclass Test{    public int addition(int a, int b){       int result = a+b;       return result;    }    public int addition (int a, int b, int c){       int result = a+b+c;       return result;    }    public static void main(String args[]){ ... Read More

What is Diamond Problem in Multiple Inheritance in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:04:18

13K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritanceThere are various types of inheritance available namely single, multilevel, hierarchical, multiple ... Read More

Make an Object Completely Encapsulated in Java

Maruthi Krishna
Updated on 02-Jul-2020 10:02:51

355 Views

The process of wrapping up the data and, the code acting on the data together is known as encapsulation. This is a protective mechanism where we hide the data of one class from (an object of) another.Since, variables hold the data of a class to encapsulate a class you need to declare the required variables (that you want to hide) private and provide public methods to access (read/write) them.By doing so, you can access the variables only in the current class, they will be hidden from other classes, and can be accessed only through the provided methods. Therefore, it is ... Read More

Does Java Support Hybrid Inheritance?

Maruthi Krishna
Updated on 02-Jul-2020 10:01:48

2K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members are created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Exampleclass Super{    int a =100;    int b = 200;    public void ... Read More

Arrange First N Natural Numbers with Absolute Difference of 1

Arnab Chakraborty
Updated on 02-Jul-2020 09:54:17

388 Views

We have the first N natural numbers. Our task is to get one permutation of them where the absolute difference between every two consecutive elements is > 1. If no such permutation is present, return -1.The approach is simple. We will use the greedy approach. We will arrange all odd numbers in increasing or decreasing order, then arrange all even numbers in decreasing or increasing orderAlgorithmarrangeN(n)Begin    if N is 1, then return 1    if N is 2 or 3, then return -1 as no such permutation is not present    even_max and odd_max is set as max even ... Read More

Advertisements