Found 9152 Articles for Object Oriented Programming

How do I declare and initialize an array in Java?

karthikeya Boyini
Updated on 19-Feb-2020 10:06:11

673 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}

How to declare an Array Variables in Java?

Sharon Christine
Updated on 19-Feb-2020 10:04:43

374 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 a composition in Java?

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

309 Views

The composition is also a type of aggregation where the relationship is restrictive i.e. If two objects are in composition, the composed object will not exist without the other.

How to declare a constructor in Java?

Sreemaha
Updated on 19-Feb-2020 07:16:09

1K+ Views

While declaring the constructors you should keep the following points in mind.A constructor does not have a return type.The name of the constructor is same as the name of the class. A class can have more than one constructor.Examplepublic class Sample {    int num;    public Sample() {       num = 30;    }    public Sample(int value) {       num = value;    } }

When should I use the keyword ‘this’ in a Java class?

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

334 Views

The this is a keyword in Java which is used as a reference to the object of the current class, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods. Example Live Demo public class This_Example { // Instance variable num int num = 10; This_Example() { System.out.println("This is an example program on keyword this"); } This_Example(int num) { ... Read More

How many public classes of the same name it can have in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

2K+ Views

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error. Example public class Example { } public class Example{ public void sample(){ System.out.println("sample method of the Example class"); } public void demo(){ System.out.println("demo method of the Example class"); } ... Read More

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

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

371 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

616 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

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

672 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

Advertisements