Object Oriented Programming Articles

Page 225 of 589

Loop through the Vector elements using an Iterator in Java

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 2K+ Views

An Iterator can be used to loop through the Vector elements. The method hasNext( ) returns true if there are more elements in the Vector and false otherwise. The method next( ) returns the next element in the Vector and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows −Exampleimport java.util.Iterator; import java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector();       vec.add(4);       vec.add(1);       vec.add(3);       vec.add(9);       ...

Read More

Find the minimum element of a Vector in Java

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 579 Views

The minimum element of a Vector can be obtained using the java.util.Collections.min() method. This method contains a single parameter i.e. the Vector whose minimum element is determined and it returns the minimum element from the Vector.A program that demonstrates this is given as follows −Exampleimport java.util.Collections; import java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector();       vec.add(7);       vec.add(3);       vec.add(9);       vec.add(5);       vec.add(8);       System.out.println("The Vector elements are: " + vec);       ...

Read More

Convert a Vector to an array in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 1K+ Views

A Vector can be converted into an Array using the java.util.Vector.toArray() method. This method requires no parameters and it returns an Array that contains all the elements of the Vector in the correct order.A program that demonstrates this is given as follows −Exampleimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector();       vec.add(7);       vec.add(3);       vec.add(5);       vec.add(2);       vec.add(8);       Object[] arr = vec.toArray();       System.out.println("The Array elements are: ");     ...

Read More

Class declaration with one method in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 418 Views

A class declaration can contain a single method. A program that demonstrates this is given as follows:Exampleclass Message {    public void messagePrint() {       System.out.println("This is a class with a single method");    } } public class Demo {    public static void main(String args[]) {       Message m = new Message();       m.messagePrint();    } }OutputThis is a class with a single methodNow let us understand the above program.The Message class is created with a single member function messagePrint(). A code snippet which demonstrates this is as follows −class Message {   ...

Read More

Why variables are declared final in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

A variable cannot be modified after it is declared as final. In other words, a final variable is constant. So, a final variable must be initialized and an error occurs if there is any attempt to change the value.A program that demonstrates a final variable in Java is given as follows −Examplepublic class Demo {    public static void main(String[] args) {       final double PI = 3.141592653589793;       System.out.println("The value of pi is: " + PI);    } }OutputThe value of pi is: 3.141592653589793Now let us understand the above program.In the main() method in class ...

Read More

Class declaration with a method that has a parameter in Java

Arushi
Arushi
Updated on 11-Mar-2026 252 Views

A class declaration can contain a method that has a parameter in Java. A program that demonstrates this is given as follows:Exampleclass Message {    public void messagePrint(String msg) {       System.out.println("The message is: " + msg);    } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint("Java is fun"); } }OutputThe message is: Java is funNow let us understand the above program.The Message class is created with a single member function ...

Read More

Duplicating Objects using a Constructor in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 451 Views

A copy constructor can be used to duplicate an object in Java. The copy constructor takes a single parameter i.e. the object of the same class that is to be copied. However, the copy constructor can only be explicitly created by the programmer as there is no default copy constructor provided by Java.A program that demonstrates this is given as follows −Exampleclass NumberValue {    private int num;    public NumberValue(int n) {       num = n;    }    public NumberValue(NumberValue obj) {       num = obj.num;    }    public void display() {   ...

Read More

Pass long parameter to an overloaded method in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 3K+ Views

Method overloading in a class contains multiple methods with the same name but the parameter list of the methods should not be the same. One of these methods can have a long parameter in their parameter list.A program that demonstrates this is given as follows −Exampleclass PrintValues {    public void print(int val) {       System.out.println("The int value is: " + val);    }    public void print(long val) {       System.out.println("The long value is: " + val);    } } public class Demo {    public static void main(String[] args) {       PrintValues ...

Read More

Methods Accepting a Variable Number of objects in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 645 Views

A method that accepts a variable number of Object arguments in Java is a form of variable length arguments(Varargs). These can have zero or multiple Object type arguments.A program that demonstrates this is given as follows:Examplepublic class Demo {    public static void Varargs(Object... args) {       System.out.println("Number of Object arguments are: " + args.length);       System.out.println("The Object argument values are: ");       for (Object i : args)       System.out.println(i);    }    public static void main(String args[]) {       Varargs("Apples", "4", "All");       Varargs("Half of", 3, "is", ...

Read More

How to extend Interfaces in Java

Arushi
Arushi
Updated on 11-Mar-2026 35K+ Views

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. An interface extends another interface like a class implements an interface in interface inheritance.A program that demonstrates extending interfaces in Java is given as follows:Exampleinterface A {    void funcA(); } interface B extends A {    void funcB(); } class C implements B {    public void funcA() {       System.out.println("This is funcA");    }    public void funcB() {       System.out.println("This is funcB");    } } public class Demo {    public ...

Read More
Showing 2241–2250 of 5,881 articles
« Prev 1 223 224 225 226 227 589 Next »
Advertisements