Categories
Programming & Dev

Understanding OOPs: The 3rd step towards Software Dev

Abstract Classes (OOPs)

First of all, there are two types of classes in OOPs, one is concrete class second is an abstract class
A concrete class is the one which we have been writing till yet (normal classes). Now, the abstract class is the one whose object cannot be initialized, only a reference can be given, that's it.

Now there are two things again abstract classes and abstract method. The major difference between them is that a class must not have an abstract method/function in order to become a non-abstract class but a class with an abstract method becomes an abstract class automatically.

Abstract method: a method with no method body only with just definition with an abstract keyword at the start

Abstract classes are generally used to standardized things, like setting a standard
you can understand more clearly from the below example

import java.util.*;

 abstract class hospital {

    abstract void emergency();
    abstract void appointment(); //now these are the guidelines or solid work set by govt for 
    abstract void admit(); //any hospital to run , thats y they are rigid/abstract here
    abstract void billing();

 }

 class myhospital extends hospital{

      public void emergency(){ //now if i want to open my own hospital i would have to 
// follow the above guidilines with changes according to me
       System.out.println("ambulance coming in 5 minutes");
      }

      public void appointment(){
          System.out.println("your appointement is booked");
      }

      public void admit(){
           System.out.println("stretcher coming in");
      }

      public void billing(){
          System.out.println("amount to be paid is $200");
      }



 }


public class Abstract {
    
    public static void main(String[] args) {
        
        hospital h=new myhospital();
        h.appointment();
        h.emergency();
        h.admit();
        h.billing();

    }
}


Let's make it more interesting to understand. Feeling hungry?

OOPs

The above code along with the explanation in the comments is a perfect example of the real-life use of Abstract classes and OOPs.

Interfaces

The interface is again a type of keyword just like abstract which when used before any class makes it a pure abstract class and the methods inside it all becomes abstract methods, so then we no more need to mention abstract before the name of methods.

interface classA{
   void billing();
   void offer();
}

class classB implements classA 
{
   
   public void billing(){
      System.out.println("GST 18%");
   }
   public void offer(){
      System.out.println("Christmas offer");
   }
}


public class interfaces {
   public static void main(String[] args) {
       
      classB c=new classB();
      c.billing();
      c.offer();

   }    
}

Output:

Understanding OOPs: The 3rd step towards Software Dev 1

Inner Classes

It is basically an implementation of the concept of nested classes. Like how a class inside another class can access the member of the outer class and how an outer class can access the member of its inner class. Also, the object creation in the main body to access both of them, For example ->

class Outer{

    int x=10;

    class Inner{

        int y=20;

        public void display(){
          System.out.println("outer class var:"+x);
          System.out.println("inner class var:"+y);
        }
    }

    void Display(){
        Inner i=new Inner();
        i.display(); //this will display both 
        System.out.println(i.y); // inner class var display
    }

 }
public class innerC {
    
    public static void main(String[] args) {
        
        Outer o=new Outer();
        o.Display();

        Outer.Inner i=new Outer().new Inner();

    }

}

Output: Understanding OOPs: The 3rd step towards Software Dev 2

Anonymous Class

An Anonymous class is a type of class that is an abstract class but is overridden at the time of its object creation
Static inner class: A class whose objects is not dependent on the outer class object which is to be initialised first. This class can be directly accessed outside that outer class.
local inner class: An inner class defined inside the method of the major class is called a local inner class

Understanding OOPs: The 3rd step towards Software Dev 3

On outputting the class will simply print "20" which is the variable of a static inner class inside the class Outer

If you have any doubts you can ask the questions directly by commenting down below. Also, don’t forget to read our 2nd article on the OOPs to understand it completely: Understanding OOPs: The second step towards Software Dev

Get the latest tech news and updatesethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.

Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *