Found 9150 Articles for Object Oriented Programming

How to deep flatten an array in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

176 Views

Flattening an arrayFlattening an array is nothing but merging a group of nested arrays present inside a provided array. Flattening of an array can be done in two ways. 1) concat.apply() In the following example there are some nested arrays containing elements 3, 4, 5 and 6. After flattening them using concat() method we get the output as 1, 2, 3, 4, 5, 6, 9. Example Live Demo var arrays = [1, 2, [3, 4, [5, 6]], 9]; var merged = [].concat.apply([], arrays); documemt.write(merged); Output1, 2, 3, 4, 5, 6, 92) array.flat()In ... Read More

Why we should follow the naming conventions in Java?

Shriansh Kumar
Updated on 27-May-2025 11:10:23

4K+ Views

The Java team and developer community suggest us to follow naming conventions. They are just conventions and are not mandatory but help in writing Java programs that are more understandable and easier to read. For example, class names should generally be nouns, and interface names should be adjectives. Additionally, capitalize the first letter of each separate word. The names used for classes, variables, and methods are called identifiers. Need to Follow Java Naming Conventions Following are the reasons to follow naming conventions: Multiple developers work on the same project simultaneously. Names that follow naming standards reduce ... Read More

What are the differences between default constructor and parameterized constructor in Java?

Shriansh Kumar
Updated on 21-May-2025 16:55:43

15K+ Views

The default and parameterized constructors are two types of Constructor in Java. The constructor is a special member of a Java class whose name is the same as the class name. It is used to assign values to a class variable at the time of object creation. In this article, we are going to discuss the difference between default and parameterized constructors. Default Constructor When we do not add a constructor to a Java class. The compiler adds a default constructor implicitly. It accepts 0 arguments. If we do not initialize the instance variables of a class, a default constructor will ... Read More

What are the different types of classes in Java?

Shriansh Kumar
Updated on 21-May-2025 16:09:47

3K+ Views

In Java, a class is a datatype which defines properties (variables) and behaviors (methods) of an object. Defining an object does not consume memory; only its object or instance does.Depending on the requirement, we will create various types of classes in Java. In this article, we are going to discuss them. Types of classes in Java The Java class is classified into different types based on its methods, as shown in the list given below: Concrete class Abstract class Final class POJO class Static class Inner Class Wrapper Class Singleton Class Concrete class Any normal class which does ... Read More

Why Object class is the super class for all classes in Java?

Shriansh Kumar
Updated on 30-May-2025 17:21:43

4K+ Views

The java.lang.Object class is the root or superclass of the class hierarchy, it belongs to the java.lang package. All predefined classes and user-defined classes are direct or indirect subclasses of the Object class. Why is Object Class a Superclass? To understand why the Object class is the superclass of every class in Java, we need to understand why a superclass is needed in the first place: To share common properties. To accept and return any type of object. Sharing Common Properties using Object Class There are 11 common properties that every ... Read More

Explain final class and final method in PHP.

Alok Prasad
Updated on 30-Jul-2019 22:30:26

1K+ Views

The final keyword is introduced by PHP5 related to the object-oriented programming concept.But before we move on to the final, we need to ensure that we have a good understanding of the inheritance concept. In inheritance, we can inherit a class from another class. Also, we can override a function in an inherited class, to replace the behavior originally provided. In some cases, we might need to keep a class from being inherited from or we may need to prevent a function to be overridden. This can be achieved with the final, by prefixing the class and function with the ... Read More

Can we declare the main () method as final in Java?

raja
Updated on 06-Feb-2020 11:11:06

5K+ Views

Yes, we can declare the main () method as final in Java. The compiler does not throw any error.If we declare any method as final by placing the final keyword then that method becomes the final method.The main use of the final method in Java is they are not overridden.We can not override final methods in subclasses.If we are using inheritance and we need some methods not to overridden in subclasses then we need to make it final so that those methods can't be overridden by subclasses.We can access final methods in the subclass but we can not override final methods.Exampleclass BaseClass ... Read More

How can I set scrollbars to never appear in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

115 Views

To set scrollbars to never appear, use the JScrollPane.HORIZONTAL_SCROLLBAR_NEVER and JScrollPane.VERTICAL_SCROLLBAR_NEVER. Let’s say you created a Box with some button components. Now, create a JScrollPane:JScrollPane scrollPane = new JScrollPane();Set the Viewport view as Box:scrollPane.setViewportView(box);Now, set the scrollbars to never appear:scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);The following is an example to set scrollbars to never appear:Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("One");       JButton button2 ... Read More

How to determine whether the JSlider is currently snapping to tick marks in Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

188 Views

At first, we will create a slider and set it to snap to tick marks:JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 40); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setSnapToTicks(true);After that, we will check whether the slider is currently snapping to tick marks. The result would be displayed in the TRUE/FALSE booleanslider.getSnapToTicks()Display the result in the Console as shown below:System.out.println("Snapping to tick marks? = "+slider.getSnapToTicks());The following is an example to determine whether the slider is currently snapping to tick marks:Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) ... Read More

How to enable the display of hidden files in a JFileChooser in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

361 Views

Set the following to FALSE to enable the display of hidden files −JFileChooser file = new JFileChooser(); file.setFileHidingEnabled(false);The following is an example to enable the display of hidden files in a JFileChooser −Examplepackage my; import javax.swing.JFileChooser; public class SwingDemo {    public static void main(String[] args) {       JFileChooser file = new JFileChooser();       file.setMultiSelectionEnabled(false);       file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);       file.setFileHidingEnabled(false);       int res = file.showOpenDialog(null);       if (res == JFileChooser.APPROVE_OPTION) {          java.io.File f = file.getSelectedFile();          System.err.println(f.getPath());       }   ... Read More

Advertisements