Found 9150 Articles for Object Oriented Programming

When to use an abstract class and when to use an interface in Java?

Shriansh Kumar
Updated on 27-May-2025 10:43:00

47K+ Views

An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods. If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java.  ... Read More

Can we create an object of an abstract class in Java?

Shriansh Kumar
Updated on 06-Jun-2025 13:59:55

15K+ Views

No, we can't create an object of an abstract class. But, we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class). An abstract class hides the implementation and shows the function definition to the user. A Java abstract class can have instance methods that implement a default behavior and 0 or more abstract methods. A method which does not have body is known as abstract method. It contains only the method signature and an abstract keyword before it as shown below: public abstract ... Read More

What is method overloading in PHP?

Alok Prasad
Updated on 31-Dec-2019 08:29:38

10K+ Views

Method Overloading is a concept of Object Oriented Programming which helps in building the composite application in an easy way. Function overloading or method overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.The above concept is fine for other programming languages and it is called static polymorphic i.e method overloading.ExampleLet's understand through an example.Output:ErrorExplanation:This will generate an error since php will say you have declared this method twice.But Other programming languages says , doTask($var1) and doTask($var1, $var2) are ... Read More

What is a constant and how to define constants in Java?

Shriansh Kumar
Updated on 06-Jun-2025 13:38:33

34K+ Views

Constants in Java A constant is a variable whose value cannot be changed once it has been initialized. Java doesn't have built-in support for constants. To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration. It is not mandatory that we assign values to constants during declaration. Syntax of Java Constant final float pi = 3.14f; The above statement declares the float variable "pi" as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. ... Read More

How to Title Case a sentence in JavaScript?

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

10K+ Views

Title Case a sentence in javascriptIt is nothing but converting first element of all the words in a sentence in to uppercase while the other elements remain in lowercase. The provided string(sentence) may contains a bunch of lowercase and uppercase elements. So we need an algorithm to Title Case the provided string.AlgorithmDivide all the words in the sentence individually. This task can be achieved by using string.split() method.Convert all the elements in each and every word in to lowercase using string.toLowerCase() method. Loop through first elements of all the words using for loop and convert them in to uppercase. After converting, ... Read More

Can we declare more than one class in a single Java program?

Shriansh Kumar
Updated on 20-May-2025 19:30:19

17K+ Views

Declaring Multiple Classes in Java Program A single Java program may contain two or more classes, it is possible in two ways: Multiple non-nested classes Nested classes The Multiple non-nested Classes We can create as many classes as we want in a single Java program but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class. When we compile a Java program with two or more classes (non-nested), the same number of .class ... Read More

How many ways to prevent method overriding in Java?

Shriansh Kumar
Updated on 21-May-2025 10:55:44

4K+ Views

Method overriding works because of the run-time method binding feature in Java. So, if we force the Java compiler to do static binding for a method then we can prevent that method from being overridden in a derived class. Preventing Method Overriding in Java We can prevent method overriding in Java in 3 ways: By declaring a method as "final" in the base class By declaring a method as "static" in the base class By declaring a method as "private" in the base class Final Methods cannot be Overridden ... Read More

Write a palindrome program in JavaScript so that only alphanumeric values should be allowed?

Vivek Verma
Updated on 29-Aug-2022 11:45:34

814 Views

Palindrome is a string that even when reversed will be the same as the original string. In simple words, the palindrome string will have odd length and the element in 0th index and the last index will be the same, element in 1st index will be the same as the second last element and so on. The palindrome contains alphanumeric elements; that means it contains the alphabet(a-z) and digits (0-9) as well. A palindrome string must look like − var str = "121Malayalam121"; Let’s understand with a suitable example. Let’s understand with a suitable example. Example 1 In the ... Read More

How can I append text to JTextArea in Java?

George John
Updated on 30-Jul-2019 22:30:26

3K+ Views

To append text to JTextArea, use the component’s append() method. Let’sa say the following is our text in the JTextArea -JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "This demonstrates the usage of JTextArea in Java. ");Now, append text to the same textArea -textArea.append("In this example we have deleted some text from the beginning."+ "We have also appended some text.");The following is an example to append text to JTextArea -Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       JTextArea textArea ... Read More

Explain difference between Abstraction and Encapsulation in PHP.

Alok Prasad
Updated on 31-Dec-2019 08:20:07

1K+ Views

PHP5 added the object-oriented programming approach with the previous version, which is used for making code reusable in real time php application.Some of the concepts of object-oriented models are: class, object, encapsulation, polymorphism, abstract and final classes, and methods, interfaces and inheritance, etc...Here we discuss the basic differences between abstraction and encapsulation.Encapsulation:Encapsulation is an approach that joins data members(variables) and implementation details into a single unit called the class that implies class is formed with variables and methods present inside it.Encapsulation is a protection mechanism for data members present inside the class i.e data members are not accessible by end ... Read More

Advertisements