Found 9124 Articles for Object Oriented Programming

How many ways can get the instance of a Class class in Java?

Monica Mona
Updated on 18-Feb-2020 09:59:52

571 Views

You can create an object of the class named Class in two ways −Using new keyword as −Class obj = new Class();Using the forName() method of the class named Class.Class obj = Class.forName("DemoTest");

How to access the private methods of a class from outside of the class in Java?

karthikeya Boyini
Updated on 18-Feb-2020 10:00:48

7K+ Views

You can access the private methods of a class using java reflection package.Step1 − Instantiate the Method class of the java.lang.reflect package by passing the method name of the method which is declared private.Step2 − Set the method accessible by passing value true to the setAccessible() method.Step3 − Finally, invoke the method using the invoke() method.Exampleimport java.lang.reflect.Method; public class DemoTest {    private void sampleMethod() {       System.out.println("hello");    } } public class SampleTest {    public static void main(String args[]) throws Exception {       Class c = Class.forName("DemoTest");       Object obj = c.newInstance(); ... Read More

What is a method in Java that ends in a semicolon and has no method body?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

756 Views

An abstract method is the one which has no definition and declared abstract. In short, an abstract method contains only method signature without body. To use this method, you need to inherit this method by extending the class and provide the method definition. Example public abstract class Employee{ private String name; private String address; private int number; public abstract double computePay(); }

What is the difference between a Java method and a native method?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

567 Views

A native method is the one whose method implementation is done in other languages like c++ and Java. These programs are linked to Java using JNI or JNA interfaces. The difference between normal method and native method is That the native method declaration contains native keyword and, the implementation of the method will be other programming language. Example Tester.java public class Tester { public native int getValue(int i); public static void main(String[] args) { System.loadLibrary("Tester"); System.out.println(new Tester().getValue(2)); ... Read More

What are native methods in Java and where are they used?

Samual Sam
Updated on 30-Jul-2019 22:30:20

2K+ Views

A native method in Java is a method whose implementation is written in other languages such as c and c++. The ‘native’ keyword is used before a method to indicate that it is implemented in other language.

When to use vararg methods in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

190 Views

Whenever, you want to pass different number of arguments each time you call a method you should use vararg methods. This example creates sumvarargs() method which takes variable no of int numbers as an argument and returns the sum of these arguments as an output. Example Live Demo public class Main { static int sumvarargs(int... intArrays) { int sum, i; sum = 0; for(i = 0; i< intArrays.length; i++) { ... Read More

What are vararg methods in Java?

Lakshmi Srinivas
Updated on 18-Feb-2020 10:02:14

246 Views

In Java methods, parameters accept arguments with three dots. These are known as variable arguments.sample(int args …){}If they are used you can pass a different number of arguments each time you call these methods.Examplepublic class Sample {    void demoMethod(String... args) {       for (String arg: args) {          System.out.println(arg);       }    }    public static void main(String args[] ) {       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap")    } }Outputram rahim robert krishna kasyap

What are variadic functions in Java?

Monica Mona
Updated on 16-Jun-2020 06:19:30

1K+ Views

Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions.ExampleLive Demopublic class Sample {     void demoMethod(String... args) {       for (String arg: args) {          System.out.println(arg);       }     }    public static void main(String args[] ){       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap");    } }Outputram rahim robert krishna kasyap

What are annotations in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

791 Views

Annotations are a tag (metadata) which provides info about a program. Annotations in Java Start with the symbol ‘@’. They are used by the compiler to detect errors. Software tools to generate code. They are used to show attributes of an element: e.g. @Deprecated, @Override. Annotation are used to describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebService Annotations describe the behaviour of an element: @Statefull, @Transaction. Example Live Demo class Sample{ public void display(){ System.out.println(" "); } } public ... Read More

What is the difference between compositions and aggregations in Java?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

212 Views

In Aggregation relationship among classes by which a class (object) can be made up of any combination of objects of other classes. It allows objects to be placed directly within the body of other classes.A composition is also a type of aggregation where the relationship is restrictive i.e. If two objects are in composition, the composed object will not exist without the other.

Advertisements