Found 9150 Articles for Object Oriented Programming

What is singleton design concept in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:38:58

4K+ Views

Singleton Pattern ensures that a class has only one instance and provides a global point to access it. It ensures that only one object is available all across the application in a controlled state. Singleton pattern provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.ExampleOutputconnection createdExplanationIn the above example as we are following a singleton pattern so the object $db2 can't be created. Only a single object will be created and i.e available all across the application.

How to modify properties of a nested object in JavaScript?

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

3K+ Views

There are two methods to modify properties of nested objects. One is Dot method and the other is Bracket method. The functionality is same for both the methods, but the only difference is their notation. lets' discuss them in detail.Dot methodExampleIn the following example initially the value of property country is England. But using Dot notation the value is changed to India.Live Demo    var person;    var txt = '';    person = {       "name":"Ram",       "age":27,       "address": {          "houseno":123,          "streetname":"Baker street",     ... Read More

Can we declare a constructor as private in Java?

Alshifa Hasnain
Updated on 05-Jun-2025 13:00:43

11K+ Views

Java constructor is a special method used to initialize objects. It has the same name as the class and is automatically called when an object is created. You can also declare a constructor as private to restrict object creation from outside the class. What is a Private Constructor? A private constructor is a constructor that is declared using the private access modifier. It restricts object creation from outside the class i.e., the class having a private constructor cannot be subclassed. Private constructors are used in design patterns like Singleton, or to prevent subclassing or instantiation. Purpose of a Private ... Read More

How to access nested json objects in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 11:42:50

14K+ Views

Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object.In the following example 'vehicles' is a object which is inside a main object called 'person'. Using dot notation the nested objects' property(car) is accessed.Example-1Live Demo    var person = {       "name":"Ram",       "age":27,       "vehicles": {          "car":"limousine",          "bike":"ktm-duke",          "plane":"lufthansa"       }    }    document.write("Mr Ram has a car called" + " " + person.vehicles.car); ... Read More

What will happen when we try to override final method of the superclass in Java?

Alshifa Hasnain
Updated on 09-Jun-2025 14:49:19

2K+ Views

Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of the super class, we will get a compile-time error.  Rules for Implementing Method Overriding The method declaration should be the same as that of the method that is to be overridden. The class (subclass) should extend another class (superclass), prior to even try overriding. The Sub Class can never override final methods of the Super Class. Final method A method is declared final when ... Read More

How to check whether an array is a true array in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 11:28:55

184 Views

In javascript, arrays are not true arrays. They are javascript objects. So when we try to know their type using typeof() operator the displayed output will be object.Syntaxtypeof(operand);parameters - typeof() operator takes an operand and returns the data type of the operand. In the following example even though variable 'a' is an array, the typeof() operator returns the output as object because in general every array is an object.ExampleLive Demo    var a = [1, 2, 5, "hello"];    document.write(typeof(a));    var b = {};    document.write("");    document.write(typeof(b)); Outputobject objectUnlike typeof() operator, Array.isArray() checks whether the passed parameter ... Read More

What is a ClassCastException and when it will be thrown in Java?

Alshifa Hasnain
Updated on 18-Apr-2025 16:12:07

2K+ Views

In this article, we will be learning about ClassCastException in Java. But before we jump into this, we'll understand what an exception is in Java. After that, we'll learn why and when ClassCastException occurs, look at real-world code examples that throw this exception, and finally learn how to avoid or resolve it. What is an Exception in Java? An exception in Java is an event that disrupts the normal flow of the program. Java provides a way to handle these errors gracefully using try-catch blocks. There are 2 types of exceptions: Checked Exceptions – ... Read More

How many ways can a property of a JavaScript object be accessed?

vineeth.mariserla
Updated on 29-Jun-2020 11:33:58

371 Views

 An object property can be accessed in two ways. One is .property and the other is [property].Syntax-1Object.property;Syntax-2Object["property"];For better understanding, lets' look at the following example.In the following example an object called 'person' is defined and its properties were accessed in a dot notation.ExampleLive Demo var person = {    firstname:"Ram",    lastname:"kumar",    age:50,    designation:"content developer" }; document.write(person.firstname + " " + "is in a role of" + " " + person.designation); OutputRam is in a role of content developerIn the following example the properties of an object 'person' were accessed in bracket notation.ExampleLive Demo ... Read More

How does Python\'s super() work with multiple inheritance?

SaiKrishna Tavva
Updated on 27-Feb-2025 19:36:45

1K+ Views

Python supports a feature known as multiple inheritance, where a class (child class) can inherit attributes and methods from more than one parent class. This allows for a flexible and powerful way to design class hierarchies. To manage this, Python provides the super() function, which facilitates the calling of methods from a parent class without explicitly naming it. Understanding Multiple Inheritance In multiple inheritance, a child class can derive attributes and methods from multiple parent classes. This can be particularly useful in scenarios where you want to compose behavior from different sources. Example In the following example, the Child class ... Read More

What are the different types of nested classes are defined in Java?

raja
Updated on 30-Jul-2019 22:30:26

1K+ Views

In Java, it is possible to define a class inside another class, such classes are called Nested classes. We can use the access modifiers like private, public, protected or default for inner classes and default or public access modifiers for outer class.There are two types of nested classes are defined in Java.Static Nested ClassNon-Static Nested ClassStatic Nested ClassWe Can define an inner class as static, so such type of classes is called a static nested class.The nested class is defined with the static keyword, so this type of nested classes doesn’t share any relationship with the instance of an outer class.A static ... Read More

Advertisements