Java Articles

Page 38 of 450

Deriving a Class in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 4K+ Views

A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance. It can also be said that it is used to extend the functionality of the class.A program that demonstrates this is given as follows:Exampleclass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A {    void funcB() {       System.out.println("This is class B");    } } public class Demo {    public ...

Read More

Creating a Multilevel Inheritance Hierarchy in Java

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 13K+ Views

Inheritance involves an object acquiring the properties and behaviour of another object. So basically, using inheritance can extend the functionality of the class by creating a new class that builds on the previous class by inheriting it.Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.A program that demonstrates a multilevel inheritance hierarchy in Java is given as follows:Exampleclass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A ...

Read More

Variable in subclass hides the variable in the super class in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 491 Views

Sometimes the variable in the subclass may hide the variable in the super class. In that case, the parent class variable can be referred to using the super keyword in Java.A program that demonstrates this is given as follows:Exampleclass A {    char ch; } class B extends A {    char ch;    B(char val1, char val2) {       super.ch = val1;       ch = val2;    }    void display() {       System.out.println("In Superclass, ch = " + super.ch);       System.out.println("In subclass, ch = " + ch);    } } ...

Read More

Using run-time polymorphism in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 479 Views

A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.A program that demonstrates run-time polymorphism in Java is given as follows:Exampleclass Animal {    void sound() {       System.out.println("Animal makes 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 {    void sound() ...

Read More

Class that contains a String instance variable and methods to set and get its value in Java

Arushi
Arushi
Updated on 11-Mar-2026 582 Views

A class declaration can contain a String instance and methods to set and get its value in Java.A program that demonstrates this is given as follows:Exampleclass Name {    private String name;    public void setName(String n) {       name = n;    }    public String getName() {       return name;    } } public class Demo {    public static void main(String[] args) {       Name n = new Name();       n.setName("John Smith");       System.out.println("The name is: " + n.getName());    } }OutputThe name is: John SmithNow let ...

Read More

What is Default access level in Java?

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 704 Views

The default access level is available when no access level is specified. All the classes, data members, methods etc. which have the default access level can only be accessed inside the same package.A program that demonstrates the default access level in Java is given as follows:Exampleclass Employee {    int empno;    String name;    void insert(int e, String n) {       empno = e;       name = n;    }    void display() {       System.out.println("Employee Number: " + empno);       System.out.println("Name: " + name);    } } public class Demo ...

Read More

Why do we use import statement in Java? Where it should be included in the class?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 5K+ Views

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.A program that demonstrates this in Java is given as follows:Exampleimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Apple");       l.add("Mango");       l.add("Cherry");       l.add("Orange");       l.add("Pear");       System.out.println("The LinkedList ...

Read More

Use a quantifier to find a match in Java

Arushi
Arushi
Updated on 11-Mar-2026 148 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:Exampleimport 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

Make a class final in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 5K+ 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:Examplefinal 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 = 15Now ...

Read More

Why Abstract Class is used in Java?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ 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:Exampleabstract 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
Showing 371–380 of 4,496 articles
« Prev 1 36 37 38 39 40 450 Next »
Advertisements