MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Abstract Class and Abstract Methods


What does Abstract mean?


Abstract in English means existing in through or as an idea without concrete existence.

Abstract class :


An abstract class cannot be instantiated.
Java requires us to extend it if we want to access it.
It can include abstract and non-abstract methods.
If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class phone Model {
             abstract void switch off (); 
              || more code 
              }

Abstract class are used when we want to achieve security & abstraction(hide certain details & show only necessary details to the user)
Example :
abstract class Phone{  
  abstract void on();  
}  
class SmartPhone extends Phone{  
void run(){
System.out.println("Turning on...");
}  
public static void main(String args[]){  
 Phone obj = new SmartPhone();  
 obj.on();  
}  
}  

Output

abstract class Phone{
abstract void on();
}
class SmartPhone extends Phone{
void run(){
System.out.println("Turning on...");
}
public static void main(String args[]){
Phone obj = new SmartPhone();
obj.on();
}
}

Abstract method :


A method that is declared without implementation is known as the abstract method.
An abstract method can only be used inside an abstract class.
The body of the abstract method is provided by the class that inherits the abstract class in which the abstract method is present.
In the above example, on() is the abstract method.

Example


package com.company;

abstract class Parent2{
    public Parent2(){
        System.out.println("Mai base2 ka constructor hoon");
    }
    public void sayHello(){
        System.out.println("Hello");
    }
    abstract public void greet();
    abstract public void greet2();
}

class Child2 extends Parent2{
    @Override
    public void greet(){
        System.out.println("Good morning");
    }
    @Override
    public void greet2(){
        System.out.println("Good afternoon");
    }
}

abstract class Child3 extends Parent2{
    public void th(){
        System.out.println("I am good");
    }
}
public class abstract {
    public static void main(String[] args) {
        //Parent2 p = new Parent2(); -- error
        Child2 c = new Child2();
        //Child3 c3 = new Child3(); -- error
    }
}


Conclusion

In this page (written and validated by ) you learned about Java Abstract Class and Abstract Methods . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Interfaces.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.