All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Abstract Class In Java Example

Last Updated : Mar 11, 2024

Abstract Class In Java Example

In this article we will show you the solution of abstract class in java example, here you have to import packages, in case you using eclipse then each program have itself.

First we declared abstract class after defined main with public class definition provided. Generally, to implement common functionality to all subclasses using abstract class. Here you can get to know clearly how to define abstract class.

Step By Step Guide On Abstract Class In Java Example :-

Package is must for each program that stores you defined class and methods with supporting imports. or else they cannot understand the code which you defined inside.

We defined abstract class 'First' the abstract-keyword denotes that is abstract class.

Then we defined main class 'second' that's extended with abstract class inside public main method specified. There we creating object for abstract class and just printed string.

//Abstract Class
package java_prgms;
abstract class First{
}
class Second extends First{
    public static void main(String args[])
    {
     First obj=new Second();
              System.out.println("This is abstract class definition");
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. We defined abstract class and the keyword is must, otherwise it will not consider as abstract class. There you can declare multiple abstract methods as much you need.
  3. You have to define main class separately with void main method definition. The main class 'Second' extends with abstract 'First' class. Then only you can execute abstract method process successfully.
  4. Because abstract class itself cannot implement method definitions. To provide access to abstract class you need to create object for that with the reference main class.
  5. Using that object you can call all abstract definied methods. Later we will see about abstract method and its definitions.
  6. Then ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  7. The ‘system.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Using print method we just printed 'This is abstract class definition' text on terminal.
  9. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to define abstract class in java. To compile java program you need to tell the java compiler which program to compile through ‘javac Second.java’.

After successful compilation you need to execute ‘java Second‘ line for gets the output.

Then you can see output 'This is abstract class definition'. On eclipse just press of run button then you will get output inside terminal.

I hope this article on abstract class in java example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪