Found 4338 Articles for Java 8

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

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

244 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

How to ensure that child class overrides a super class method in java?

Maruthi Krishna
Updated on 01-Aug-2019 11:55:07

572 Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation (body) to it. If a class contains at least one abstract method, you must declare it abstract.In other words, if you extend an abstract class it is mandatory to implement (override) all the abstract methods in it or, declare it abstract else a compile time error will be generated for each abstract method (that you ... 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

What is the reason behind the error “Could not found or load main class” 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

Default method vs static method in an interface in Java?

Maruthi Krishna
Updated on 02-Jul-2020 10:22:11

7K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces.Default Methods - Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void ... 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

How to make an object completely encapsulated in java?

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

288 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

Is it mandatory to override the default methods of an interface in Java?

Maruthi Krishna
Updated on 01-Aug-2019 11:12:37

2K+ Views

The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void display() {       System.out.println("display method of MyInterface");    } } public class InterfaceExample implements MyInterface{    public static void main(String ... Read More

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

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

11K+ 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

Write a program to convert an array to String in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

278 Views

The Arrays class of the java.util package provides a method named toString() this method accepts an array value (of any type) and returns a String.ExampleFollowing Java program accepts various arrays from the user, converts them into String values and prints the results. Live Demoimport java.util.Arrays; import java.util.Scanner; public class ObjectArrayToStringArray {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       //Integer array to String       System.out.println("Enter 5 integer values: ");       int intArray[] = new int[5];       for(int i=0; i

Advertisements