Abstract Classes in Java

 Java Abstraction


    In Java, abstraction is the practice of keeping a code's implementation details hidden and just providing the user with the information they need to know. It offers the capacity to reduce complexity and ignore unimportant information in order to simplify complex systems.

    Modifying a class or a method using the abstract keyword can also be called abstraction.

Abstract methods:-

  • abstract keyword is used to declare the method as abstract.
  • You have to place the abstract keyword before the method name in the method declaration.
  • An abstract method contains a method signature, but no method body(meaningless method).



 Abstract classes:-

  • if a class has at least one abstract method, then the class must be declarer as abstract class.
  • You have to place the abstract keyword before the class name in the class declaration.
  • Java abstract classes may or may not contain abstract methods.
  • Cannot be final.
  • Older days we cant create constructors for abstract classes. But nowadays we can create constructors for the abstract classes(after java 8).



    We cant create objects for abstract classes directly. We need to create a subclass that extends the abstract class and then create an instance of that subclass.



    If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

    After inheriting, we can run methods in abstract class using super keyword. We can upcasting and run abstract class methods.
    
Example 01-



Output:-



Example 02-

Output:-



Advantages in java  abstraction:-

    Java Abstraction offers numerous advantages such as code reusability, secure code, robust system design, flexibility, and enhanced modularity. It is used in real-world applications across various domains, including Database Connectivity (JDBC) and Graphical User Interface (GUI) applications.
    

Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points