Found 9150 Articles for Object Oriented Programming

How to call an interface method in Java?

raja
Updated on 11-Feb-2020 08:06:59

22K+ Views

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.Examplepublic interface InterfaceDemo{     default public void displayNameDefault(String name){        System.out.println("Your name is : " + name);    }     public void displayName(String name);     public void displayNameAndDesignation(String name, String designation); }The above interface defines three methods for displaying a name and optionally a job title. One method is a default method that contains implementation logic. The remaining two methods do not include implementation logic.public class InterfaceDemoImpl ... Read More

What is the use of Array.findIndex() method in JavaScript?

Abdul Rawoof
Updated on 25-Aug-2022 12:26:49

461 Views

Array is a data type which can store multiple elements of similar data types. For example, if array is declared as integer data type then it stores one or more elements of the integer data type. In JavaScript, Arrays are objects and these objects have some inbuilt functions and properties by which one can do the operations faster and easier. In this tutorial a functionality of array object ‘findIndex()’ is explained and demonstrated with some examples. The ‘findIndex()’ method in JavaScript arrays will return the first element from the given array with the given constraint being satisfied. This method will ... Read More

What is the use of Array.Find() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

294 Views

Array.find()Array.find() is used to return value of first element in the array that satisfies provided testing condition(user given condition).If the provided testing condition fails then array.find() returns undefined.In the following example array.find() checks whether the price elements in array are more than the given testing price(12000). If the provided testing condition true then first value that passed the test will be executed, if not undefined will be executed. ExampleLive Demo    var price = [3000, 21000, 28000, 20000, 15500];    function checkCost(cost) {       return cost >= 12000;    }    document.getElementById("price").innerHTML = price.find(checkCost); Output21000

Can we declare an abstract method, private, protected, public or default in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:45:25

12K+ 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 to it.Declaring an abstract method privateIf a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.Therefore, the abstract method cannot ... Read More

Can we declare a main method as private in Java?

raja
Updated on 11-Feb-2020 07:49:21

5K+ Views

Yes, we can declare the main method as private in Java.It compiles successfully without any errors but at the runtime, it says that the main method is not public.Example:class PrivateMainMethod {    private static void main(String args[]){        System.out.println("Welcome to Tutorials Point");     } }The above code is working successfully at compile time but it will throw an error at the runtime.Output:Error: Main method not found in class PrivateMainMethod, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Can we execute a java program without a main method?

raja
Updated on 07-Oct-2023 02:45:53

31K+ Views

Yes, we can execute a java program without a main method by using a static block.  Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block. Static initialization block is going directly into the stack memory. Example class StaticInitializationBlock{    static{       System.out.println("class without a main method");       System.exit(0);    } } In the above example, we can execute a java program without a main method (works until Java 1.6 version). ... Read More

Can we declare an abstract method final or static in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:04

12K+ 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 to it.Declaring abstract method staticIf you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.If you still, try to declare an abstract method static a compile time error is generated saying ... Read More

Can we create an object for the abstract class in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:39

2K+ 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 to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Instantiating an abstract classOnce a class is abstract it indicates that it may contain incomplete methods hence you cannot create an object of the abstract class.If you try ... Read More

Can we define an abstract class without abstract method in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:37:31

6K+ 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 to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete ... Read More

Is it possible to catch multiple Java exceptions in single catch block?

Maruthi Krishna
Updated on 29-Jun-2020 13:39:07

1K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeBefore Java 7 whenever we have a code that may generate more than one exception and if you need to handle the specifically you should use multiple catch blocks on a single try.ExampleThe following Java program contains an array of numbers (which is displayed). from user it accepts two positions from this array and, divides the number in first position ... Read More

Advertisements