Found 9128 Articles for Object Oriented Programming

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

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

232 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

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

Niharikaa Aitam
Updated on 29-May-2025 05:19:23

2K+ Views

Python allows dictionaries to be sorted using the built-in sorted() function. In this article, we will explore different ways to sort a dictionary in Python by its values. Sort a Dictionary Using itemgetter() method When we use the sorted() function along with the itemgetter() method of the operator module, the dictionary will be sorted by its values. The itemgetter() function returns a callable object that fetches an item from its operand using the provided index. Example Following is an example that shows how to sort a dictionary by its values using itemgetter() along with the sorted() function - from operator import ... Read More

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

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

186 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();

How to create an object from class in Java?

Prabhas
Updated on 19-Feb-2020 07:39:44

2K+ Views

An object is created from a class using the new keyword. There are three steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Examplepublic class Sample {    public static void main(String args[]){       Sample s = new Sample();    } }

What is the equivalent of C# namespace in Java?

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

331 Views

Packages are similar to namespaces in C#.

Advertisements