- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
163 Views
The hashCode() method is used to obtain the hash code for a string. This method does not accept any parameters as it is a default method and it returns a hash code value.A program that demonstrates the hashCode() method in Java is given as follows:Example Live Demoimport java.io.*; public class Demo { public static void main(String args[]) { String str = new String("The sky is blue"); System.out.println("The string is: " + str); System.out.println("The Hashcode for the string is: " + str.hashCode()); } }OutputThe string is: The sky is blue The ... Read More
991 Views
When a garbage collector determines that no more references are made to a particular object, then the finalize() method is called by the garbage collector on that object. The finalize() method requires no parameters and does not return a value.A program that demonstrates the finalize() method in Java is given as follows:Example Live Demoimport java.util.*; public class Demo extends GregorianCalendar { public static void main(String[] args) { try { Demo obj = new Demo(); System.out.println("The current time is: " + obj.getTime()); obj.finalize(); ... Read More
4K+ Views
A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.A program that demonstrates this is given as follows:Example Live Democlass Student { private int rno; private String name; public Student(int r, String n) { rno = r; name = n; } public String toString() { return rno + " " + name; } } public class Demo { public static void main(String[] args) { Student ... Read More
706 Views
Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class methods sqrt() and pow() in the package java.lang are static imported. A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo { public static void main(String args[]) { double num = 4.0; System.out.println("The number is: " + num); System.out.println("The square root of the above number is: " + sqrt(num)); ... Read More
14K+ Views
Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class method sqrt() in the package java.lang is static imported.A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; public class Demo { public static void main(String[] arg) { double num = 36.0; System.out.println("The number is: " + num); System.out.println("The square root of the above number is: " + sqrt(num)); } }OutputThe number is: 36.0 The square root ... Read More
11K+ Views
All the classes in a package can be imported using the import statement along with the character *. For example - All the classes in the package java.util can be imported using import java.util.*;A program that demonstrates this in Java is given as follows:Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Stack stack = new Stack(); stack.push("Apple"); stack.push("Mango"); stack.push("Guava"); stack.push("Pear"); stack.push("Orange"); System.out.println("The stack elements are: " + stack); } }OutputThe stack elements ... Read More
72K+ Views
An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.A program that demonstrates multiple inheritance by interface in Java is given as follows:Example Live Demointerface AnimalEat { void eat(); } interface AnimalTravel { void travel(); } class Animal implements AnimalEat, AnimalTravel { public void eat() { System.out.println("Animal is eating"); } public void travel() { System.out.println("Animal is travelling"); ... Read More
214 Views
An interface can be defined using the interface keyword. It contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. An interface is primarily used to implement abstraction and it cannot be instantiated.A program that demonstrates an interface in Java is given as follows:Example Live Demointerface AnimalSound { abstract void sound(); } class CatSound implements AnimalSound { public void sound() { System.out.println("Cat Sound: Meow"); } } class DogSound implements AnimalSound { public void sound() { System.out.println("Dog Sound: Bark"); } } ... Read More
695 Views
A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.A program that demonstrates an abstract class in Java is given as follows:Example Live Demoabstract class Animal { abstract void sound(); } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal ... Read More
4K+ Views
A class can be made final by using the final keyword. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.A program that demonstrates a final class in Java is given as follows:Example Live Demofinal class A { private int a = 15; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } }OutputValue of a = ... Read More