All TalkersCode Topics

Follow TalkersCode On Social Media

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

Abstract Method In Java Example

Last Updated : Mar 11, 2024

Abstract Method In Java Example

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

We declared abstract class with abstract void method declaration. Then we defined main class, which contains abstract method definition and public void main method definition. Generally, abstract class itself cannot implement functionality so you need to define on subclasses.

Step By Step Guide On Abstract Method 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. Here we defined abstract class 'Absblock' with abstract method dis() and abstract-keyword denotes that is abstract properties or assets.

Then we defined main class 'Sample' that's extended with abstract class for implement the abstract method by main class object.

//Abstract Method Example
package Examples;
abstract class Absblock{
 abstract void dis();
}
class Sample extends Absblock{
 void dis(){
     System.out.println("Printed From Abstract Method...");
 }
    public static void main(String args[])
    {
     Absblock obj=new Sample();
     obj.dis();
    }
}
  1. The ‘Package Examples’ 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. At the foremost define abstract class ‘Absblock’ and method ‘dis’ the keyword ‘abstract’ is must, otherwise it will not treated as abstract property. There you can declare multiple abstract methods as much as you need.
  3. The main class ‘Sample’ defined, which is extends with abstract class ‘Absblock’. Here the ‘extends’ keyword refers relation between two classes. Without that you cannot get output and it throws error.
  4. To access or proceed abstract method code execution we need to define object for abstract class then you can call your function with that.
  5. That’s why we created object ‘obj’ for class ‘Absblock’ by pointing main class ‘Sample’. After creation you have to call abstract method, it refers below line of ‘obj.dis();’.
  6. The dis() method will get load on server and displaying message ‘Printed From Abstract Method...’ on terminal. Without object you cannot access your method definition, so carry fully check you have done everything properly.
  7. Then ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  8. 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.
  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 and access abstract method in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Sample.java’.

After successful compilation you need to execute ‘java Sample‘line for gets the output. Then you can see output ' Printed From Abstract Method...'.

On eclipse just press of run button then you will get output inside terminal.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪