Found 9128 Articles for Object Oriented Programming

How to loop through an array in Java?

karthikeya Boyini
Updated on 26-Feb-2020 10:12:36

494 Views

To process array elements, we often use either for loop or for each loop because all of the elements in an array are of the same type and the size of the array is known. Suppose we have an array of 5 elements we can print all the elements of this array as −ExampleLive Demopublic class ProcessingArrays {    public static void main(String args[]) {       int myArray[] = {22, 23, 25, 27, 30};             for(int i=0; i

What is the difference between compile time polymorphism and runtime polymorphism in java?

usharani
Updated on 30-Jul-2019 22:30:20

1K+ Views

If we perform (achieve) method overriding and method overloading using instance methods, it is run time (dynamic) polymorphism. In dynamic polymorphism the binding between the method call an the method body happens at the time of execution and, this binding is known as dynamic binding or late binding. Example Live Demo class SuperClass{ public static void sample(){ System.out.println("Method of the super class"); } } public class RuntimePolymorphism extends SuperClass { public static void sample(){ System.out.println("Method of the ... Read More

How to process Arrays in Java?

Sharon Christine
Updated on 16-Jun-2020 10:10:14

2K+ Views

To process array elements, we often use either for loop or for each loop because all of the elements in an array are of the same type and the size of the array is known. Suppose we have an array of 5 elements we can print all the elements of this array as:ExampleLive Demopublic class ProcessingArrays {    public static void main(String args[]) {       int myArray[] = {22, 23, 25, 27, 30};       for(int i = 0; i

Difference between Method Overloading and Method Overriding in Java

Kiran Kumar Panigrahi
Updated on 14-Apr-2025 10:40:50

38K+ Views

Java supports overloading, i.e., in a Java class, we can have two different methods with the same name and different parameter lists (different number of parameters with same type or, same number of parameters with different types).  Whereas, method overriding is a concept where the super class and the sub-class have the same methods, including the parameters and signature. The JVM calls the respective method based on the object used to call the method. Method Overloading in Java When a class has two or more methods with the same name but different parameters, the respective method is called based on ... Read More

What is the difference between method overloading and method hiding in Java?

varun
Updated on 30-Jul-2019 22:30:20

1K+ Views

method hiding − When super class and the sub class contains same methods including parameters, and if they are static and, when called, the super class method is hidden by the method of the sub class this is known as method hiding. Example Live Demo class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo{ public static void demoMethod() { System.out.println("method of sub class"); } ... Read More

How to find Min/Max numbers in a java array?

Samual Sam
Updated on 07-Oct-2023 02:52:33

31K+ Views

You can find the minimum and maximum values of an array using for loops −ExampleLive Demopublic class MinAndMax {    public int max(int [] array) {       int max = 0;             for(int i=0; imax) {             max = array[i];          }       }       return max;    }    public int min(int [] array) {       int min = array[0];             for(int i=0; i

Is final method inherited in Java?

seetha
Updated on 30-Jul-2019 22:30:20

2K+ Views

No, we cannot override a final method in Java. The final modifier for finalizing the implementations of classes, methods, and variables. We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot modify a final method from a sub class. The main intention of making a method final would be that the content of the method should not be changed by any outsider. Example class Demo{ public final void demoMethod(){ System.out.println("Hello"); } } public class Sample extends Demo{ ... Read More

How to create and populate two-dimension Java array?

Sai Subramanyam
Updated on 16-Jun-2020 09:53:19

801 Views

A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns −Example Live Demopublic class Creating2DArray { public static void main(String args[]) { int[][] myArray = new int[3][3]; myArray[0][0] = 21; myArray[0][1] = 22; myArray[0][2] = 23; myArray[1][0] = 24; myArray[1][1] = 25; myArray[1][2] = 26; myArray[2][0] = 27; myArray[2][1] = 28; myArray[2][2] = 29; for(int i=0; i

How to create and populate Java array of Hash tables?

Lakshmi Srinivas
Updated on 16-Jun-2020 09:52:15

607 Views

One way to create an array of hash tables is to create Hashtable objects and to assign these to a Hashtable array using curly braces:ExampleLive Demoimport java.util.Hashtable; public class HashTableOfArrays {    public static void main(String args[]) {       Hashtable ht1 = new Hashtable();       ht1.put("Name", "Krishna");       ht1.put("Age", "28");       ht1.put("City", "Visakhapatnam");       ht1.put("Phone", "9848022338");             Hashtable ht2 = new Hashtable();       ht2.put("Name", "Raju");       ht2.put("Age", "30");       ht2.put("City", "Chennai");       ht2.put("Phone", "9848033228");     ... Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
Updated on 19-Dec-2019 08:54:20

417 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.ExampleLive Demoimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

Advertisements