Found 9320 Articles for Object Oriented Programming

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

12K+ 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

404 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?

Swarali Sree
Updated on 19-Feb-2020 10:03:30

2K+ Views

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −type[] reference = new type[10];Where, type is the data type of the elements of the array.reference is the reference that holds the array.And, if you want to populate the array by assigning values to all the elements one by one using the index −reference [0] = value1; reference [1] = value2;For example, if you want to create an integer array of 5 elements you can create it using new keyword −int[] myArray = new int[5]; You ... Read More

Does a constructor have a return type in Java?

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

4K+ 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?

Syed Javed
Updated on 30-Jul-2019 22:30:22

5K+ Views

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. Character ch = new Character('a'); Following are the notable methods of the Character class. 1 isLetter() Determines whether the specified char value is a letter. 2 isDigit() Determines whether the specified char value is a digit. 3 isWhitespace() Determines whether the specified char value is white space. 4 isUpperCase() Determines ... 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

2K+ 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

Can a class in Java be both final and abstract?

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

1K+ Views

An abstract cannot be instantiated. Therefore to use an abstract class you need to create another class and extend the abstract class and use it. If a class is final you can’t extend it further. So, you cannot declare a class both final and abstract. Example Still if you try to do so you will get a compile time error saying “illegal combination of modifiers:” final abstract class Demo{ public final void display(){ System.out.println("Hello welcome to tutorialspoint"); } } Output C:\Sample>javac Demo.java Demo.java:1: error: illegal ... Read More

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

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

288 Views

The Number class (abstract) of the java.lang package represents the numeric values that are convertible to primitive types byte, double, float, int, long, and short. Following are the method provided by the Number class of the java.lang package. Sr.No Method & Description 1 byte byteValue() This method returns the value of the specified number as a byte. 2 abstract double doubleValue() This method returns the value of the specified number as a double. 3 abstract float floatValue() This method returns the value of the specified number as a float. ... Read More

Advertisements