Found 9122 Articles for Object Oriented Programming

What is the super() construct of a constructor in Java?

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

462 Views

The super keyword is similar to this keyword. Following are the scenarios where a super keyword is used. It is used to differentiate the members of superclass from the members of the subclass if they have same names. It is used to invoke the superclass constructor from the subclass. Whenever you want to call the constructor of the superclass from a method or another constructor you can do so as: Example class Person { Person(String name) { System.out.println("Hello "+ name); } } class Student ... Read More

How to declare, create, initialize and access an array in Java?

Sharon Christine
Updated on 25-Feb-2020 10:56:19

315 Views

You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}

What is the purpose of a default constructor in Java?

varma
Updated on 12-Mar-2020 05:21:59

2K+ Views

Default constructors in Java:A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. There are two types of constructors namely −parameterized constructors − Constructors with arguments.no-arg constructors − Constructors without arguments.Example Live Demopublic class Sample{    int num;    Sample(){       num = 100;    }    Sample(int num){       this.num = num;    }    public static void main(String args[]){       System.out.println(new Sample().num);       System.out.println(new Sample(1000).num);    } }Output100 1000Default ConstructorIt ... Read More

Can a constructor be overridden in java?

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

15K+ Views

If super class and sub class have same methods including name, return type and parameters, and if you try to call it using the object of the sub class Then the method in the sub class is invoked. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class’s constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error. Example ... Read More

What is the class "class" in Java?

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

507 Views

The Java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor. Example Following is the example demonstrates the usage of the class Class. The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name. Live Demo import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = ... Read More

Can constructors be marked final, abstract or static in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

2K+ Views

Except public, protected and, private constructor does not allow any other modifier. When you use a final keyword with a method or constructor it cannot be overridden. But, a constructor in Java cannot be overridden therefore, there is no need of using the final keyword with the constructor. Since you cannot override a constructor you cannot provide body to it if it is made abstract. Therefore, you cannot use abstract keyword with the constructor. If you want to invoke a member of a class before instantiating the class you need to use static before it. But, contractors are called ... Read More

How to create an Array in Java?

Alshifa Hasnain
Updated on 07-Mar-2025 17:38:36

3K+ Views

In this article, we will learn to create an Array in Java. Arrays provide an efficient way to manage and access data collections, making them essential for many programming tasks. What is an Array? A Java array is an ordered, fixed-size collection of elements of the same data type. Each element in an array is kept at a specific index, beginning with 0. Java arrays may store primitive data types (int, double, char) and objects (like String, Integer). Creating an Array In Java, you can create an array just like an object using the new keyword. The syntax of creating ... Read More

Does a constructor have a return type in Java?

Monica Mona
Updated on 30-Jul-2019 22:30:20

5K+ Views

No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. Example If you closely observe the declaration of the constructor in the following example it just have the name of the constructor which is similar to class and, the parameters. It does not have any return type. public ... Read More

What is the character wrapper class and its methods in Java?

Alshifa Hasnain
Updated on 26-Mar-2025 12:06:18

6K+ Views

In this article, we will learn about the character wrapper class and its methods in Java. The Character class is the primitive char type wrapper, providing useful methods for manipulation, classification, and conversion of characters. The Character Wrapper Class The Character class of the java.lang package wraps a value of the primitive datatype char. It offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor. Syntax Character ch = new Character('a'); Various Methods in the Character Class The following are some of the methods used in the ... Read More

Should a constructor always have the same name as the class in java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:22

3K+ Views

Yes, the constructor should always have the same name as the class. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. Example Live Demo public class Sample{ public Sample(){ System.out.println("This is a constructor"); } public static void main(String args[]){ Sample obj = new Sample(); } } Output This is a constructor

Advertisements