Found 9150 Articles for Object Oriented Programming

Does Java support multiple inheritance? Why? How can we resolve this?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

5K+ Views

Whenever, you extend a class a copy of superclass’s members is available to the subclass object and, when you can call the method of the superclass using the object of the subclass.ExampleIn the following example, we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo(). Live Democlass SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public ... Read More

What are the modifiers allowed for methods in an Interface in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

4K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.In Java 7As of Java7 you can have only public, abstract as modifiers for the methods of an interface.interface MyInterface{ public abstract void display(); public abstract void setName(String name); public abstract void setAge(int age); }Using any other modifier with the methods of an interface would lead to a compile time error.From Java8From Java8 onwards interfaces ... Read More

Can we declare the method of an Interface final in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.By default, all the methods of an interface are public and abstract. For example, In the following Java program, we are having a declaring a method with name demo.public interface MyInterface{ void demo(); }If you compile this using the javac command as shown below −c:\Examples>javac MyInterface.javait gets compiled without errors. But, if you verify the interface after compilation using the javap ... Read More

Explain naming conventions for packages in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

While choosing a package name you need to keep the following points in mind.The name of the package should be in small letters.It is suggested to start the name of the package with the top level domain level followed by sub domains, ex: com.example.tutorialspoint.ExampleYou can declare a package as −package com.mypackage.tutorialspoint; Public class Sample{ Public static void main(String args[]){ System.out.println("Welcome to Tutorialspoint"); } }Executing a packageYou need to compile the file with packages using –d option as −C:\Sample>javac -d . PackageExample.javaAfter compiling, you can see the class files ... Read More

How can we make JTextField accept only numbers in Java?

Alshifa Hasnain
Updated on 08-May-2025 18:45:58

15K+ Views

In this article, we will learn to make a JTextField accept only numbers in Java. By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. Different Approaches The following are the two different approaches for making a JTextField accept only numbers in Java: Using a KeyListener Using DocumentFilter Using a KeyListener The KeyListener interface handles keyboard events, making it straightforward to ... Read More

Can we override final methods in Java?

Narasimha Murthi
Updated on 30-Jun-2020 07:12:47

1K+ Views

Overriding is a one of the mechanisms to achieve polymorphism. This is the case when we have two classes where, one inherits the properties of another using the extends keyword and, these two classes same method including parameters and return type (say, sample).Since it is inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the subclass.When we invoke this method (sample) JVM calls the respective method based on the object used to call the method.Overriding final methodsNo, you cannot override final method in java. If ... Read More

Can I overload static methods in Java?

Narasimha Murthi
Updated on 30-Jul-2019 22:30:26

1K+ Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Example Live Demopublic class Calculator {    public int addition(int a , int b){       int result = a+b;       return result;    }    public int addition(int a , int b, int c){       int result = a+b+c;       return result;    }    public static void main(String args[]){       Calculator ... Read More

Can I overload private methods in Java?

Narasimha Murthi
Updated on 30-Jul-2019 22:30:26

2K+ Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Overloading private methodsYes, we can overload private methods in Java but, you can access these from the same class.Example Live Demopublic class Calculator {    private int addition(int a , int b){       int result = a+b;       return result;    }    private int addition(int a , int b, int c){       int result = ... Read More

What is System.exit() in Java?

Narasimha Murthi
Updated on 30-Jul-2019 22:30:26

320 Views

This method belongs to the System class of the java.lang package. It terminates the current JVM (Java Virtual Machine).This method accepts an integer value representing the status code, it accepts two values 0 or, 1 or, -1. Where, 0 indicates a successful termination and 1 or, -1 indicates an unsuccessful termination.ExampleFollowing program accepts an array of elements from the user and prints the it. While printing if any of the given elements is greater or equal to 20 the program exits. Live Demoimport java.util.Scanner; public class System_Exit_Example {    public static void main(String args[]){       Scanner sc = new ... Read More

How can I search a character or substring in java?

Narasimha Murthi
Updated on 30-Jul-2019 22:30:26

253 Views

You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.Example Live Demopublic class Test {    public static void main(String args[]){       String str = new String("hi welcome to Tutorialspoint");       int index = str.indexOf('w');       System.out.println("Index of the letter w :: "+index);    } }OutputIndex of the letter w :: 3

Advertisements