Found 9320 Articles for Object Oriented Programming

Can we overload the main method in Java?

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

3K+ Views

Yes, we can overload the main method in Java, but When we execute the class JVM starts execution with public static void main(String[] args) method. Example Live Demo public class Sample{ public static void main(){ System.out.println("This is the overloaded main method"); } public static void main(String args[]){ Sample obj = new Sample(); obj.main(); } } Output This is the overloaded main method

Why is method overloading not possible by changing the return type of the method only in java?

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

2K+ Views

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. If you observe the following example, it contains two methods with same name, different parameters and if you call the method by passing two integer values the first method will be executed and, if you call by passing 3 integer values then the second method will be executed.It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method. Example ... Read More

What is method hiding in Java and how to use it?

Giri Raju
Updated on 30-Jul-2019 22:30:20

2K+ Views

When super class and sub class contains same method including parameters and if they are static. The method in the super class will be hidden by the one that is in the sub class. This mechanism is known as method hiding. Example Live Demo class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo { public static void demoMethod() { System.out.println("method of sub class"); } public static void main(String args[] ) { Sample.demoMethod(); } } Output method of sub class

What is the difference between method hiding and method overriding in Java?

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

1K+ Views

When super class and the sub class contains same instance methods including parameters, when called, the super class method is overridden by the method of the sub class. WIn this example super class and sub class have methods with same signature (method name and parameters) and when we try to invoke this method from the sub class the sub class method overrides the method in super class and gets executed. Example Live Demo class Super{ public void sample(){ System.out.println("Method of the Super class"); } } public ... Read More

Why can’t we override static methods in Java?

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

5K+ Views

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call.Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

What is an array data structure in Java?

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

157 Views

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which is used to identify the element. Example Live Demo public class ArrayExample { public static void main(String args[]){ int myArray[] = {44, 69, 89, 635}; for (int i = 0; i

What are the types of arrays in Java?

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

15K+ Views

There are two types of arrays in Java they are − Single dimensional array − A single dimensional array of Java is a normal array where, the array contains sequential elements (of same type) − int[] myArray = {10, 20, 30, 40} Example Live Demo public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements ... Read More

What is a Multidimensional array in Java?

Monica Mona
Updated on 07-Mar-2024 16:48:43

2K+ Views

In Java, a multi-dimensional array is nothing but an array of arrays.2D array − A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns −Int[][] myArray = {{10, 20, 30}, {11, 21, 31}, {12, 22, 32} }In short, a two-dimensional array contains one-dimensional arrays of elements. It is represented by two indices where the first index denotes the position of the array and the second index represents the position of the element within that particular array − Example ... Read More

How to sort a dictionary in Python by values?

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

2K+ Views

Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first added. >>> from collections import OrderedDict >>> D = {5:'fff', 3:'ttt', 1:'ooo', 4:'bbb', 2:'ddd'} >>> OrderedDict(D.items())  OrderedDict([(5, 'fff'), (3, 'ttt'), (1, 'ooo'), (4, 'bbb'), (2, 'ddd')]) We also need to us sorted() function that sorts elements in an iterable in a specified order. The function takes a function ... Read More

What are all the ways an object can be created in Java?

varun
Updated on 16-Jun-2020 09:03:59

138 Views

You can create an objectUsing new keyword.Sample obj = new Sample();Using the newInstance() method and Class.forName() method.Sample obj2 = (Sample) Class.forName("Sample").newInstance();Using the clone() method by implementing Cloneable Interface (marker).Sample obj3 = (Sample) obj1.clone();Using class loader.Object obj4 = Sample.class.getClassLoader().loadClass("Sample");Using the constructor class from lang.reflect.Class cls = Sample.class; Constructor obj = cls.getDeclaredConstructors()[0]; Sample obj5 = (Sample) obj.newInstance();

Advertisements