Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 25 of 50

What is the reason behind the error “Could not found or load main class” in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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
Maruthi Krishna
Updated on 02-Jul-2020 4K+ 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

How do you prevent a method from getting overridden in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 421 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

Can we call a constructor directly from a method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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
Maruthi Krishna
Updated on 02-Jul-2020 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

What is diamond problem in case of multiple inheritance in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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

How to make an object completely encapsulated in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 392 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
Maruthi Krishna
Updated on 02-Jul-2020 3K+ 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

What is a remote interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 4K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ...

Read More

Can we declare the variables of a Java interface private and protected?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 4K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Private fields of an interfaceIf the fields of the interface are private, you cannot access them in the implementing class.If you try to declare the fields of an interface private, a compile time error is generated saying “modifier private not allowed here”.ExampleIn the following Java example, we are trying to declare the field and method of an interface private.public interface MyInterface{    private static final int num = 10;    private abstract void demo(); }Compile time errorOn compiling, the above program ...

Read More
Showing 241–250 of 500 articles
« Prev 1 23 24 25 26 27 50 Next »
Advertisements