Found 9326 Articles for Object Oriented Programming

What does Integer.parseInt() method do in Java?

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

315 Views

This is a static method of the class named Integer it accepts an integer parameter and Parses it as a signed decimal integer. Example Live Demo public class IntegerDemo { public static void main(String[] args) { // parses the string argument int a = Integer.parseInt("12"); int b = Integer.parseInt("26"); int c = Integer.parseInt("54"); int m = a * b * c; System.out.print("Value after multiplying = " + m); } } Output Value after multiplying = 16848

What is the difference between super and this, keywords in Java?

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

512 Views

The this is a keyword in Java which is used as a reference to the object of the current class. Using it you can − Differentiate the instance variables from local variables if they have same names, within a constructor or a method. Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation. Example class Superclass { int age; Superclass(int age) { this.age = age; } public void ... Read More

Can constructors be inherited in Java?

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

3K+ Views

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors. Example public interface InterfaceTest { public InterfaceTest(){ } public abstract void display(); public abstract void show(); } Still, if you try to write constructors in an interface it will generate a compile time error. Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected InterfaceTest(){ ^ 1 error C:\Sample>

Can interfaces have constructors in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

530 Views

No, interfaces can’t have constructors for the following reasons − All the members of an interface are abstract, and since a constructor cannot be abstract. Still, if you try to write a constructor within an interface it will generate a compile time error. Example public interface InterfaceTest { InterfaceTest(){ } public abstract void display(); public abstract void show(); } Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected public InterfaceTest(){ ^ 1 error

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

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

363 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

240 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

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

400 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

Advertisements