Found 9150 Articles for Object Oriented Programming

Pass long parameter to an overloaded method in Java

Rishi Raj
Updated on 30-Jun-2020 08:41:58

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 −Example Live Democlass 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) {       ... Read More

Duplicating Objects using a Constructor in Java

Rishi Raj
Updated on 30-Jun-2020 08:44:29

399 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 −Example Live Democlass NumberValue {    private int num;    public NumberValue(int n) {       num = n;    }    public NumberValue(NumberValue obj) {       num = obj.num;    }    public void display() { ... Read More

Are Multiple Constructors possible in Java?

Aishwarya Naglot
Updated on 30-Jun-2020 08:45:40

12K+ Views

Constructors in Java are special methods that are used to initialize objects. In Java, it is possible to have multiple constructors in a class; this is known as constructor overloading. So, the answer to the question "Are multiple constructors possible in Java?" is yes, Java allows multiple constructors in a class. This feature is useful for creating objects in different ways, depending on the parameters passed during object creation. Parameters should be different in type or number to distinguish between the constructors. Example of Multiple Constructors in Java In this example, we will create a class named `Book` with multiple ... Read More

Role of Matcher.group() method in Java Regular Expressions

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

673 Views

The method java.time.Matcher.group() is used to find the subsequence in the input sequence string that is a match to the required pattern. This method returns the subsequence that is matched by the previous match which can even be empty.A program that demonstrates the method Matcher.group() in Java regular expressions is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("\w\d"); Matcher m = p.matcher("This is gr8"); System.out.println("The input ... Read More

Class declaration with a method that has a parameter in Java

Arushi
Updated on 30-Jul-2019 22:30:24

210 Views

A class declaration can contain a method that has a parameter in Java. A program that demonstrates this is given as follows:Example Live Democlass 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 ... Read More

Using final keyword to Prevent Overriding in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

2K+ Views

Method overriding can be prevented by using the final keyword with a method. In other words, a final method cannot be overridden.A program that demonstrates this is given as follows:Exampleclass A {    int a = 8;    final void print() {       System.out.println("Value of a: " + a);    } } class B extends A {    int b = 3;    void print() {       System.out.println("Value of b: " + b);    } } public class Demo { public static void main(String args[]) { B ... Read More

Why variables are declared final in Java

Rishi Raj
Updated on 30-Jun-2020 08:32:59

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 −Example Live Demopublic 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 ... Read More

Use Pattern class to match in Java

Vikyath Ram
Updated on 30-Jun-2020 08:33:56

226 Views

The representation of the regular expressions are available in the java.util.regex.Pattern class. This class basically defines a pattern that is used by the regex engine.A program that demonstrates using the Pattern class to match in Java is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("p+");       Matcher m = p.matcher("apples and peaches are tasty");       System.out.println("The input string is: apples and peaches are tasty");       System.out.println("The Regex is: p+ ");       System.out.println();     ... Read More

Class declaration with one method in Java

Vikyath Ram
Updated on 30-Jun-2020 08:34:50

368 Views

A class declaration can contain a single method. A program that demonstrates this is given as follows:Example Live Democlass 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

Role of Matcher.find(int) method in Java Regular Expressions

Rishi Raj
Updated on 30-Jun-2020 08:37:18

273 Views

The Matcher.find(int) method finds the subsequence in an input sequence after the subsequence number that is specified as a parameter. This method is available in the Matcher class that is available in the java.util.regex package.The Matcher.find(int) method has one parameter i.e. the subsequence number after which the subsequence is obtained and it returns true is the required subsequence is obtained else it returns false.A program that demonstrates the method Matcher.find(int) in Java regular expressions is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = ... Read More

Advertisements