Found 34486 Articles for Programming

How to import all classes in Java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

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

Multiple inheritance by Interface in Java

Arushi
Updated on 02-Sep-2023 10:19:21

73K+ 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

What does Interface consist of in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

217 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

Why Abstract Class is used in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:24

712 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

Make a class final in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

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

Use a quantifier to find a match in Java

Arushi
Updated on 30-Jul-2019 22:30:24

65 Views

One of the quantifiers is the plus(+). This matches one or more of the subsequence specified with the sequence.A program that demonstrates using the quantifier plus(+) to find a match in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("o+");       Matcher m = p.matcher("o oo ooo");       System.out.println("The input string is: o oo ooo");       System.out.println("The Regex is: o+ ");       System.out.println();       while (m.find()) {          System.out.println("Match: ... Read More

Use find() to find multiple subsequences in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

95 Views

The find() method finds the multiple subsequences in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex packageA program that uses the find() method to find multiple subsequence in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("the");       Matcher m = p.matcher("eclipses of the sun and the moon");       System.out.println("Subsequence: the");       System.out.println("Sequence: eclipses of the sun and the moon");   ... Read More

Why Final Class used in Java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

779 Views

The final keyword is used with a class to create a final class. 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 = 9;    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 ... Read More

Why Protected access modifier used in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:24

172 Views

The data members and methods of a class can be accessed from the same package or sub-classes in a different package if they are specified with the protected access modifier. The keyword protected is used to specify this modifier.A program that demonstrates the protected access modifier in Java is given as follows:Example Live Democlass A {    protected int a = 9;    public void printA() {       System.out.println("Value of a = " + a);    } } public class Demo {    public static void main(String args[]) {       A obj = new A();     ... Read More

Demonstrate Private access modifier in Java

Arushi
Updated on 30-Jul-2019 22:30:24

128 Views

The data members and methods of a class can be accessed only from within their declared class if they are specified with the private access modifier. The keyword private is used to specify this modifier.A program that demonstrates the private access modifier in Java is given as follows:Example Live Democlass A {    private int a = 6;    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();    } ... Read More

Advertisements