Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

OOP, OOPS Concept in Java:-

Object-oriented programming structure, It is used to create real-world based applications using dynamic memory allocation, security, reusability, and extend-ability features.



Rules for OOP'S:-

1 Class and Object:-

Class:-   It is used to define the characteristics of an object using data member and member function.

we can define multiple objects definitions using a class but all objects should be related to each other.

class Classname
{
      Data member;

      Member Function;

       Constructor;

       Static Block;

       Initializes block;

       Property;

       class Classname{}

       Object
 

}


Object:-  It is a real-world entity that has an identity, state, and behavior.

for example, Electronics is the class which has multiple objects such as Laptop, Mobile, Television
, Camera etc.


How We Create Real-world Java Program?


1  Business Class:- this class contain only object definition


2 Executable Class:- 
 this class contains main() to call any other class object.


Program Explanation Of Student Class:-

Business Class:-

import java.util.Scanner;
class Student
{
   int no;
   String sname;
   Scanner sc = new Scanner(System.in);
   void accept()
   {
      System.out.println("enter rno");
      rno=sc.nextInt();
      System.out.println("enter name");
      sname=sc.next(); 
       

   }
   void display()
   {
       System.out.println("rno is "+rno);
       System.out.println("name is "+sname); 
   }

}


Executable Class:-


class StudentMain
{
    public static void main(String args[])
    {
        Student obj = new Student();
        obj.accept();
        obj.display();
        Student obj1 = new Student();
        obj1.accept();
        obj1.display();     

    }

}

For Multiple Object using Student Object Array 
 
class StuMain
{
    public static void main(String args[])
    {
        int num,r;
        String n;
        System.out.println("enter number of students");
        Scanner sc = new Scanner(System.in);
        num=sc.nextInt();        
        Student obj[]= new Student[num];
        for(int i=0;i<num;i++)
        {
             System.out.println("Enter element for "+i+" index");
             obj[i]=new Student();
             System.out.println("enter rno");
             r=sc.nextInt();
             System.out.println("enter name");
             n=sc.next();
             obj[i].accept(r,n);
            
        }
        System.out.println("Student information is ");
       for(int i=0;i<num;i++)
       {
            obj[i].display();
       }

    }

}


ASSIGNMENT:-

CREATE CLASS TO MANAGE EMPLOYEE INFORMATION USING EMPID, EMP NAME, JOB, SALARY.


MARKSHEET PROGRAM USING FIVE DIFFERENT STUDENT AND FIND MAX 
OBTAINED MARKS.



Component of Class in Java:-

1 Data member or Variable:-

It is called variable and constant of class, it is used to declare attribute of Object 
We can declare two different types of the attribute, class type attribute will be declared by static data members of the class.
The instance attribute will be declared by the dynamic data members of the class.

Static Data Member:-

It will allocate memory at compile-time and declare by static keyword. Static Data Member will be called by Classname.

static datatype variable-name = value

it is used to create a shared variable for multiple users in real-world applications.

all local variables which will be declared under main() or any other static method then it will be static.

if we want to declare a global static variable underclass then we use the static keyword.

static datatype identifier=value;

static int x=100;


class Staticdemo
{
   static int x=10;   //global static
   static int y=20;
   public static void main(String args[])
   {
      int z=22;      //local static
      x=100;
       
      System.out.println(x+y);

   } 



}

class StaticExample
{
    static int x,y,z;
    static void addition()
    {
           x=100;
           y=200;
           z=x+y;
           System.out.println(z);
    }

    public static void main(String args[])
    {
          addition();

   }

}

Static Example With Different Class:-

class Staticdemo
{
   static int x=10;
   static int y=20;
   


}

class StaticMain
{
public static void main(String args[])
   {
      int z=22;
      Staticdemo.x=100;
       
      System.out.println(Staticdemo.x+Staticdemo.y);

   } 

}





Another Example of Static:-

class StaticExample
{
    static int x=100,y=209,z;
    static void addition()
    {
          
           x=100;
           y=200;
           int x=20;
           z=x+y;
           System.out.println(z);
    }

   

}

class StaticMain
{
     public static void main(String args[])
    {
          StaticExample.addition();
           System.out.println(StaticExample.x);

     }


}

2  Instance variable or Dynamic variable or non-static variable:-


This type of variable will allocate memory at run time and it will be called by Object.

If we want to allocate different memory for different user's then we use the instance variables.

class Classname
{
      datatype identifier;
      

}


when we declare any variable under the instance method or underclass without static keyword then it will be a dynamic variable.


class A
{
      int x,y;

}


Explanation by program 

class DynamicDemo
{
    int x,y;
    public static void main(String args[])
    {
        DynamicDemo obj = new DynamicDemo();
        obj.x=100;
        obj.y=200;
        System.out.println(obj.x+obj.y);

    }   


}

Another Example:-

class StaticExample
{
     int x=100,y=209,z;
     void addition()
    {
          
           x=100;
           y=200;
           int x=20;
           z=x+y;
           System.out.println(z);
    }

   

}

class StaticMain
{
     public static void main(String args[])
    {
          StaticExample ram = new StaticExample();
         
          ram.addition();
           System.out.println(ram.x);

          StaticExample shyam = new StaticExample();
          
          shyam.addition();
          shyam.x=300;
           System.out.println(shyam.x);

     }


}

2 Member function or Method:-

Member function and method both are the same according to object orientation terminology we will tell member function to the method.


It is used to define data members of a class using member function. if data member will be static
then we create a static member function and if data members will be dynamic then we will create a dynamic member function.

because we should never access data members directly, it should be defined by member function for security concern.


class A
{
   static int a,b=3,c; //static,class
   int x=10,y=20,z;  //instance,dynamic
   static void additionFunction()
   {
        a=1000;
        b=2000;
        c=a+b;
        System.out.println(c);
   } 
   void subFunction()
    {
        x=100;
        y=200;
        z=x-y;
        System.out.println(z);
        
    }
   public static void main(String ram[])
   {
       A obj= new A();  //new --> dynamic memory allocation 
       obj.additionFunction();
       obj.subFunction();
       
   }

}


type of member function:-



1 static member function


   static returntype methodname()
   {

   }


it will be called by Class name

Complete Explanation of static component of class:-

class StaticDemo
{
    private static int x,y;
    static void addition()
    {
       x=100;
       y=200;
       System.out.println(x+y);

    }  
    

}

class Staticmain
{
   public static void main(String args[])
    {
          StaticDemo.addition();
        

    }   


}

common interview question for the static method:-


when we call static data member or static member function using object then what will be the output?


It will be successful to compile and execute and provide proper output but data not assigned into runtime memory it will be assigned into compile-time memory. it is not the prefered way to call static because unwanted memory will be created.














2 dynamic member function
  
   returntype methodname()
   {

   }

It will be called by the object

Example of dynamic:-
class DynamicDemo
{
    private int x,y;
    void addition()
    {
       x=100;
       y=200;
       System.out.println(x+y);

    }  
    public static void main(String args[])
    {
        DynamicDemo obj = new DynamicDemo();
        obj.addition();
        

    }   


}

Type of Member Function According to Parameters:-

1 Default:-


We can not pass any value from calling function to called function. Input will be defined under the function block.


Type of Default Function:-

1.1 With Return Type:-

it will return output data which can be used by any other program.

AccessSpecifier Datatype  Methodname()
{
  
       return  output;

}

public int fun()
{
    return 10;
}


public static int  fun()
{
    return 10;
}


1.2 Without Return Type:-

We can not return any output data from the calling function to called function.

Input and output both will be defined under the function block.


acessspecifier  void functionname()
{


}


public void fun()
{
   int a=100;
   int b=200;
  System.out.println(a+b);

}

public static void fun()
{
    int a=100;
   int b=200;
  System.out.println(a+b);
}



2 Parametrized:-

using this we can pass value from calling function to called function using a parameter.

parameter identifier is called arguments.


With Return Type:-

accessspecifier  returntype methodname(datatype arguments)
{


}

Without Return Type

access specifier  void method name(datatype arguments)
{


}


Complete Explanation of Method:-


class MethodExample
{
    int a,b;
    void fun1()
    {
       a=10;
       b=20;
       System.out.println(a+b);
        
    }

    int fun2()
    {
         a=100;
         b=20;
         return a-b;

    } 
    void fun3(int a,int b)
    {
         System.out.println(a+b);
    } 
   
    int fun4(int a,int b)
    {
         return a+b;
    } 


}

class Methodmain
{
   public static void main(String args[])
   {
        MethodExample obj = new MethodExample();
        obj.fun1();
        int res = obj.fun2();
        System.out.println(res); 
        obj.fun3(100,20);
        int res1 = obj.fun4(100,2);
        System.out.println(res1); 
           

   }


}


Example of Static and Dynamic member function in Single Class:-

class MF
{
   private static int a,b;
   private int x,y;
   
   static void addition()
   {
       MF o = new MF();
       a=100;  
       b=200;
       o.x=100;
       o.y=200;
       System.out.println("static addition"+(a+b));
       System.out.println("dynamic addition"+(o.x+o.y));
      
   } 
    void sub()
   {
    
       MF.a=10;  
       MF.b=20;
       x=100;
       y=200;
       System.out.println("static addition"+(MF.a+MF.b));
       System.out.println("dynamic addition"+(x+y));
      
   } 

  


}
class MFMain
{
     public static void main(String args[])
     {
       MF.addition();
       MF obj2 = new MF();
       obj2.sub();
       
     }

} 



3 Constructor:-

It is used to Initialize dynamic data members of a class, Constructor name, and class name both will be the same.

A constructor has no user define a return type, it will be the return address of class type.

when we compile any class then the constructor block will be created automatically.

Actual class:-
class A
{


}

After compilation
class A
{
     A()
    {


   }

}

Note:-  Default Constructor by default present into class. it will be called when we create an Object.


TYPE   OF CONSTRUCTOR:-

1)  Default or no parameter:-

    The default constructor has also two different type

   1)  Class type default Constructor

   2)  User define default Constructor

    we can not pass any value from an object

2) Parameterized constructor

   we can pass parameter using object

   2.1)  One parameter, Two Parameter,...

  2.2)   Reference param or Object Param,.......

Example of Constructor with the complete program:-

class ConsDemo
{
   int a,b;
   ConsDemo()
   {
      a=100;
      b=200;
   }  
  ConsDemo(int a,int b)
   {
       this.a=a;
       this.b=b;
   }  

   ConsDemo(ConsDemo obj)
   {
       this.a=obj.a;
       this.b=obj.b; 

   } 
   void display()
   {
      System.out.println(a*b);
   }

}
class ConsMain
{
     public static void main(String args[])
     {
          ConsDemo obj = new ConsDemo();
          obj.display();
          ConsDemo obj1 = new ConsDemo(10,20);
          obj1.display();
          ConsDemo obj2 = new ConsDemo(obj1);
          obj2.display(); 

      }


}


Another Example of Constructor:-

class Consdemo
{
     int a;
     Consdemo()
     {
         a=2;
     } 
     Consdemo(int a)
     {
        this.a=a;
     }

    Consdemo(Consdemo obj)
    {
       this.a=obj.a;

    }
    void displaySquare()
    {
        System.out.println("square is "+a*a);
    }

public static void main(String args[])
{
    Consdemo obj = new Consdemo();
    obj.displaySquare();
    Consdemo obj1 = new Consdemo(12);
    obj1.displaySquare();
    Consdemo obj2 = new Consdemo(obj1);  //obj2=obj
    obj2.displaySquare();
    
}

}


Another Example using a static variable:-

 
4 Static Block:-

It is used to initialize static data members of the class. The static block will be called before main() but the constructor will be called after main()  when we create an object.

static block is also called an anonymous block on Java program?



class StaticBlockExample
{
   static int a,b;
   static
   {
      a=100;
      b=200; 
      System.out.println("Static Block");

   }
   static void display()
   {
      System.out.println(a+b);
   }

   public static void main(String args[])
   {
      StaticBlockExample.display();
   
   } 



}


Another Program of Static Block to UnderStand:-

class Stblock
{
    static int a=10;
    static
    {
      a=5;
    }
    public static void main(String args[])
    {
       System.out.println(a);
    }
     static
    {
      a=1;
    }
}
 


5 Init Block:-

This block is used to initialize any data member before the constructor means if the program has a static block, constructor, and init block then the first static then main(), init, and after that constructor will be called.


Complete Program with an explanation of StaticBlock, Init, Constructor, and main()

Init block will never call without object creation because it will always call before the constructor.


class StaticBlockExample
{
   static int a,b;
   int x,y;
   {
       System.out.println("INIT");
      x=10;
      y=20;
   }
   StaticBlockExample()
   {
      System.out.println("CONSTRUCTOR");
      System.out.println(x+y);
   }
    

   static
   {
      a=100;
      b=200; 
      System.out.println("Static Block");

   }
   static void display()
   {
      System.out.println(a+b);
   }

   public static void main(String args[])
   {
      System.out.println("Main");
      //StaticBlockExample obj = new StaticBlockExample();
     // StaticBlockExample.display();
   
   } 



}


Example of Init Block and Constructor:-

class A
{
  int a,b;

  {
     a=100;
     b=200;
     
   }
   
   A()
   {
      System.out.println(a+b);
   }

   public static void main(String args[])
   {
       A obj = new A();
   }

   
  {
     a=10;
     b=20;
    

  }

}


.....................................................................................................................................


Another Program for Static Block, Init Block, Constructor?


class Stblock
{
    static int a=10;
    int b=200;
    Stblock()
    {
      System.out.println("Default Constructor"+b);
    }
    {
         b=5;
    }
    static
    {
      a=5;
    }
    public static void main(String args[])
    {
       System.out.println(a);
       Stblock obj = new Stblock();
      
    }
     static
    {
      a=1;
    }
}



6 Properties

7 Inner class

8 Object





2 Data Abstraction and Data Encapsulation:-

Abstraction means Data hiding, it is used to hide non-essential details and show only essential details in an application.

Abstraction is used to provide security and accessibility options in the project.

In Java programming language we can implement data abstraction using three different ways.

1) Using Access Specifier:-

1.1 private:-  it is used to provide accessibility only in the same class. we can not access private data members into another class in the same package and different packages.

class A
{
   private int a;
   private void fun()
   {

   }

}
note:-  outer class never will be private in java because the class-default scope is the "default" modifier.




1.2 default:- 

 it is used to provide accessibility in the same package all classes.

  int b;
  void fun1()
  {

  }

if we do not write any access modifier then "default" will be considered



3 protected:-  


it can be accessed in other package classes but inheritance is required. without inheritance, it is similar to the default.

protected int a;
protected void fun2()
{

}

4 public:- it can be accessed in other projects and the cross packages also, it has more strength as compared to other access specifiers.

  public int a;
  public void fun3()
  {

  }


2) Using Abstract Class


3) Using Interface


Data Encapsulation:- Binding of data in a single unit, encapsulation is used to protect the direct accessibility of data members, it provides accessibility of data members using member function.



class A
{
    public int a=10,b=20;  //not encapsulated
     int a=100,b=200;    // wrong 
   
}
class A
{
    private int a,b;  //encapsulation
    void fun()  //fun() encapsulate a and b
    {
     a=10;
     b=20;
    }


}


note:-  these rules of oops is used to provide security and accessibility option in the program.


Live example of ATM Operation using data abstraction and data encapsulation:-

import java.util.Scanner;
class Bank
{
   int bal=5000;
   Scanner sc  = new Scanner(System.in);
   private void credit(int amt)
   {
      bal+=amt;  
 
   }

   private void debit(int amt)
   {
      bal-=amt;
   }

   private void checkbalance()
   {
       System.out.println("balance is "+bal);
   }
   
   private int accept()
     {
         System.out.println("Enter amount");
         int amt = sc.nextInt();
         return amt;
     }

   void login(int pin)
   {
       if(pin==1234)
        {
            while(true)
            {
          System.out.println("press c for credit \n press d 

for debit \n press b for balance \n press e for balance");
            char ch = sc.next().charAt(0);
            switch(ch)
            {
            case 'c':
            credit(accept());
            break;
            case 'd':
            debit(accept());
            break;
            case 'b': 
            checkbalance();
            break;
            case 'e': 
            System.exit(0);
            break;
            default:
            System.out.println("wrong choice");
            break; 
            }
            }

        }

   }
   

}


class Customer
{
   public static void main(String args[])
   {
         Bank obj = new Bank();
      // obj.credit(1200);
         System.out.println("Enter pin code");
         int pin = obj.sc.nextInt();
         obj.login(pin);
   }

}

Polymorphism:-


Poly means many and "morphism" means forms using this we can increase the usability of function and operator in the program.

Polymorphism is used to provide overloading and overriding features.

Java does not support operator overloading because we can not re-define the operator definition.


We can reduce the extra memory space of the program using polymorphism.


Type of Polymorphism:-

1 Static Polymorphism:-  it will perform the operation at compile time hence it is also early binding.

1.1 function overloading:-

We will create the same name function only the parameters will be different, constructor is the best example of function overloading,

println() is the example of function overloading.

package p1;

public class Operation {
  void add(int a,int b)
  {
      System.out.print(a+b);
  }
  void add(int a,float b)
  {
      System.out.print(a+b);
  }
  void add(float a,int b)
  {
      System.out.print(a+b);
  }
  void add(float a,float b)
  {
      System.out.print(a+b);
  }
  public static void main(String args[])
  {
      Operation obj = new Operation();
   //   obj.add(100,200);
     obj.add(100.2F,200.2F);
  }
}

2 Dynamic Polymorphism:-  it will perform operation at runtime using inheritance.

2.1 function overriding


Create the Same name method from base class to derived class only functionality will be different.

note:-  function name  and return type should be the same from base class to derived class 

class A
{
    void fun()
    {

    }

}
class  B extends A
{
  void fun()  //correct
    {

    }
 
  int fun()  //wrong
    {

    }




1 using Abstract class

2 using interface 

3 using normal class

A complete example of Overriding:-

class Exam
{
   public void boardInfo()
   {
         System.out.println("RGPV");   
   }
   public void examPattern()
   {
        System.out.println("Number System");
   }

}
class Examnew extends Exam
{
   public void examPattern(String s)
   {
        System.out.println("Grade System"+s);
   }
   public void examPattern()
   {
         System.out.println("Grade System");
   }

    public static void main(String args[])
     {

        // Exam obj = new Examnew();
        // Exam obj = new Exam();
         Examnew obj = new Examnew();
         obj.boardInfo();
         obj.examPattern();
      }

}






Program Explanation:-
................................................................................................................................

class SI
{
    float p,r,t,si;
    void accept(float p,float r,float t)
    {
        System.out.println("FLOAT,FLOAT,FLOAT"); 
        this.p=p;
        this.r=r;
        this.t=t;
    }
    void accept(int p,int r,int t)
    {
        System.out.println("INT,INT,INT");
        this.p=p;
        this.r=r;
        this.t=t;
    }
    void accept(int p,float r,int t)
    {
        System.out.println("INT,FLOAT,INT");
        this.p=p;
        this.r=r;
        this.t=t;
    }
    void accept(int p,float r,float t)
    {
        System.out.println("INT,FLOAT,FLOAT");
        this.p=p;
        this.r=r;
        this.t=t;
    }
    void accept(float p,int r,int t)
    {
        System.out.println("FLOAT,INT,INT");
        this.p=p;
        this.r=r;
        this.t=t;
    }

    void calcsi()
    {

        si = (p*r*t)/100;
    }
    void add()
    {
       si = (p+r+t);
    } 

    void display()
    {

       System.out.println(si);
    } 

}
class SIMain
{
     public static void main(String args[])
     {
        SI obj = new SI();
        obj.accept(2,35.45F,4);
        obj.calcsi();
        obj.display(); 
       
     }



}


...............................................................................................................................................................


Class and Object Related Program to Calculate Simple Interest?
.......................................................................................................................
class SI
{
    float p,r,t,si;
    void accept(float p,float r,float t)
    {
        this.p=p;
        this.r=r;
        this.t=t;
    }

    void calcsi()
    {

        si = (p*r*t)/100;
    }
    void add()
    {
       si = (p+r+t);
    } 

    void display()
    {

       System.out.println(si);
    } 

}
class SIMain
{
     public static void main(String args[])
     {
        SI obj = new SI();
        obj.accept(12000.2F,2.2F,2.2F);
        obj.calcsi();
        obj.display(); 
        SI obj1 = new SI();
        obj1.accept(12000.2F,2.2F,2.2F);
        obj1.add();
        obj1.display(); 

     }


}

..............................................................................................................................................................

Inheritance:-
.............................................................................................................................................................

It means re-usability, using this concept of oops we can adopt the features of the base(parent) class to derived(child) class and reduces common code from an application.

Type of Inheritance:-


1 Single:-  Base class to derived class

Base


Derived


2 Multilevel:-    Base class to derived class to sub derived


Base


Derived


Sub-Derived



3 Hierarchical:-     it will provide inheritance using tree structure from base class to subderived1,subderived2

                                                          Base


          Derived1                                                             Derived2






Example of Single Inheritance:-

class Admin
{
    int aid;
    String aname; 
    void accept(int aid, String aname)
    {
       this.aid = aid;
       this.aname=aname;

    }
    void display()
    {
       System.out.println("aid "+aid+" name is "+aname);

    }

}

class Employee extends Admin
{
     int salary;
     void accept1(int salary)
     {
        this.salary=salary;
     }
     void display1()
     {
       System.out.println("Salary is "+salary);
     }

}

class EmpMain
{
    public static void main(String args[])
    {
       System.out.println("Employee Info");
       Employee obj = new Employee();
       obj.accept(1001,"emp");
       obj.accept1(12000);
       obj.display();
       obj.display1();
       System.out.println("Admin Info");
       Admin obj1 = new Admin();
       obj1.accept(1000,"admin");
       obj1.display();
       

    }


}


Practical Explanation of Multilevel Inheritance using Admin--->EMP--->OtherStaff?




public class Admin {
int aid;
String sname;
void accept(int aid,String sname)
{
    this.aid=aid;
    this.sname=sname;
}
void display()
{
    System.out.print("id is "+aid);
    System.out.print("name is "+aid);
}

}
 


......................................


public class Employee extends Admin {
int salary;
void accept1(int salary)
{
    this.salary=salary;
   
}
void display1()
{
    System.out.print("Salary is "+salary);
   
}

}
.....................................


public class OtherStaff extends Employee{
    int bonus;
    void accept2(int bonus1)
    {
        bonus=bonus1;
    }
    void display2()
    {
        System.out.println("Bonus is "+bonus);
    }
   

}
EmpMain Class for Accessbility:-


package p1;

public class EmpMain {

    public static void main(String[] args) {
       
        System.out.println("Employee Information");
        //Employee obj = new Employee();
        OtherStaff obj= new OtherStaff();
        obj.accept(1001,"manish kumar");
        obj.accept1(12000);
        obj.display();
        obj.display1();
        System.out.println("\nAdmin Information");
        //Admin obj1 = new Admin();
        //Employee obj1 = new Employee();
        OtherStaff obj1= new OtherStaff();
        obj1.accept(1000, "admin");
        obj1.display();
       
        System.out.println("\nOtherStaff Information");
        OtherStaff obj2 = new OtherStaff();
        obj2.accept(1002, "other staff");
        obj2.accept1(6000);
        obj2.accept2(100);
        obj2.display();
        obj2.display1();
        obj2.display2();
       

    }

}
 


Example of Hierarchical Inheritance:-

public class Admin {
int aid;
String sname;
void accept(int aid,String sname)
{
    this.aid=aid;
    this.sname=sname;
}
void display()
{
    System.out.print("id is "+aid);
    System.out.print("name is "+aid);
}

}
 


......................................


public class Employee extends Admin {
int salary;
void accept1(int salary)
{
    this.salary=salary;
    
}
void display1()
{
    System.out.print("Salary is "+salary);
    
}

}
.....................................


public class OtherStaff extends Admin{
    int bonus;
    void accept2(int bonus1)
    {
        bonus=bonus1;
    }
    void display2()
    {
        System.out.println("Bonus is "+bonus);
    }
    

}

....................................................................................................................................
Main Class Code in Java:-


public class EmpMain {

    public static void main(String[] args) {
        
        System.out.println("Employee Information");
        Employee obj = new Employee();
      
        obj.accept(1001,"manish kumar");
        obj.accept1(12000);
        obj.display();
        obj.display1();
        System.out.println("\nAdmin Information");
        //Admin obj1 = new Admin();
        //Employee obj1 = new Employee();
        OtherStaff obj1= new OtherStaff();
        obj1.accept(1000, "admin");
        obj1.display();
        
        System.out.println("\nOtherStaff Information");
        OtherStaff obj2 = new OtherStaff();
        obj2.accept(1002, "other staff");
      
        obj2.accept2(100);
        obj2.display();
    
        obj2.display2();
        

    }



Abstract class in Java:-
............................................................................................


It is the special class of JAVA which is used to contain a set of abstract methods to hide actual details.

we can not create an object of an abstract class, it will be extended by Child class.

abstract class use function overriding by default because all abstract methods must be defined.
we will use the abstract keyword to declare an abstract class and abstract method.

abstract method:-   "it is the special method in java which has the only declaration, it does not contain method body"

the abstract method only will be declared under abstract class and interface in java.



Syntax of Abstract Class:-

 abstract class Classname


      variable;
       abstract access-specifier return-type method-name();
       accessspecifier returntype methodname()
      {

      }

}


 We can create the only reference for abstract class, not object.


note:-  abstract class contain main() but we can not create an object of abstract class but another class object can be created.






Advantage of an abstract class:-

1)  Data abstraction:-   abstract class provides class level data abstraction.


2) Data Contract:-  all methods of an abstract class must be defined by the child class. it is used to provide a high-level design approach for project development.




Example of Abstract class:-


abstract class Bank
{
     String s;
     public void info()
     {
       s="SBI";
       System.out.println("welcome in "+s);
     }
     abstract void credit(int amt);
     abstract void debit(int amt);
     abstract void checkbalance();     

}
class BankTransaction extends Bank
{
void checkbalance()
{
System.out.println("balance ");
}
void debit(int amt)
{
System.out.println("debit amount is "+amt);
}

void credit(int amt)
{
System.out.println("credit amount is  "+amt);
}

public static void main(String args[])
{
  // Bank obj = new BankTransaction();  //ok

     BankTransaction obj = new BankTransaction();  //ok

//    Bank obj = new Bank();   // not ok
  
   obj.info();
   obj.credit(12000);

}
  
}


Another Example of Abstract class?

for example, if we want to create a device then how many features will be implemented in the Mobile class that will be declared by Abstract class and it must be implemented by Child class.


abstract class MyDevice
{
   void deviceInfo()
    {
      System.out.println("welcome in mobile device ");
    }
   abstract void camera();
   abstract void multimedia();
   abstract void calling();

}


class Mobile extends MyDevice   //concrete class
{
void calling()
{
System.out.println("calling ");
}
void multimedia()
{
System.out.println("multimedia");
}

void camera()
{
System.out.println("camera ");
}
public static void main(String ar[])
{

  MyDevice obj = new Mobile();
  obj.calling();
  obj.multimedia();

}
}

An abstract class is used to implement a design pattern in java project where we use the top layer, middle layer, and user layer, top layer is used to declare a set of methods and the middle layer will implement it that can be possible with abstract class and interface.

.....................................................................................................................................................................
Final Class:-
.....................................................................................................................................................................

This is a special class that can not be inherited.

final class A
{

}

class B extends A  //error because the final can not be inherited
{

}

Note:-  final is the modifier of Java which is used to restrict the variable to override value, restrict method from function overriding, and restrict class for inheritance.


final int a=10;
final void display()
{

}


Interface:-

It is a pure abstract class because Interface only contains a set of abstract methods.

but now interface some features have been modified now we can write the default method and static method under the interface.



this feature has been added in JDK 1.8.


The interface not contain any protected, default private dynamic method, and constructor.




It is also used to declare a set of abstract method which will be implemented by the Middle Layer of The project.



Interface Support Multiple Inheritance because we can implement two interfaces in One class.



interface Interfacename
{
default void methodName()
{

}

static void fun()
{

}


void methdoname();  #abstract method
 

}


no need to write abstract and public keyword, it will be by default public and abstract:-


interface I
{
int a=10;  //final variable
default void methodName()  //default
{
  System.out.println("default");
}

static void fun()  //static
{
System.out.println("fun");
}
void fun1();  //abstract

}
interface I1
{
  void fun2();
}
class A implements I,I1
{
   public void fun1()
   {
      System.out.println("fun1");
   }
   public void fun2()
   {
      System.out.println("fun2");
   }
  public static void main(String args[])
  {
      I obj = new A();
      obj.methodName();
      I.fun();
      I1 obj1 = new A();
      obj1.fun2();
    
  }

}




Latest Example of Interface and class

interface Area
{
   final int a=10;
   static void fun()
   {
      System.out.println("Static");
   }
   default void fun2()
   {
      System.out.println("default");
   }
   void triangle();
   void rectangle();
}
interface Maths
{
   void multi();
}
class A
{

}
class B
{

}
class Impl extends A implements Area,Maths
{
   public void multi()
  {
    System.out.print("TAREA");
  }
  public void triangle()
  {
    System.out.print("TAREA");
  }
  public void rectangle()
  {
    System.out.print("RAREA");
  }
public static void main(String args[])
  {
     Area obj = new Impl();
     obj.triangle();
     obj.rectangle();
  }
}


Modified Example:-

interface Area
{
   final int a=10;
   static void fun()
   {
      System.out.println("Static");
   }
   default void fun2()
   {
      System.out.println("default");
   }
   void triangle();
   void rectangle();
}
interface Maths extends Area
{
   void multi();
}
abstract class A
{

}
class B
{

}
class Impl extends A implements Maths
{
   public void multi()
  {
    System.out.print("TAREA");
  }
  public void triangle()
  {
    System.out.print("TAREA");
  }
  public void rectangle()
  {
    System.out.print("RAREA");
  }
public static void main(String args[])
  {
     Area obj = new Impl();
     obj.triangle();
     obj.rectangle();
  }
}

3 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. program of employee

    import java.util.Scanner;
    class Emp
    {
    Scanner sc=new Scanner(System.in);
    int empid,sal;
    String name,job;
    void accept()
    {
    System.out.println("Enter Employee Id");
    empid=sc.nextInt();
    System.out.println("Enter Employee Name");
    name=sc.next();
    System.out.println("Enter Employee Job");
    job=sc.next();
    System.out.println("Enter Employee Salary");
    sal=sc.nextInt();
    }
    void display()
    {
    System.out.println(empid);
    System.out.println(name);
    System.out.println(job);
    System.out.println(sal);
    }
    }
    class Empmain
    {
    public static void main(String args[])
    {
    Emp e=new Emp();
    e.accept();
    e.display();

    }
    }

    ReplyDelete
  2. import java.util.Scanner;
    class Marksheet
    {
    private int roll,ph,ch,eh,mh,total;
    private String name;
    void get(int roll,String name,int ph,int ch,int eh,int mh)
    {
    this.roll=roll;
    this.name=name;
    this.ph=ph;
    this.ch=ch;
    this.eh=eh;
    this.mh=mh;
    total=ph+ch+eh+mh;
    }
    void show()
    {
    System.out.println();
    System.out.println("Roll no = "+roll);
    System.out.println("Name = "+name);
    System.out.println();
    System.out.println("Physics Score= "+ph+"/100");
    System.out.println("Chemistry Score= "+ch+"/100");
    System.out.println("English Score = "+eh+"/100");
    System.out.println("Maths Score = "+mh+"/100");
    System.out.println();
    System.out.println("Total Marks Obtained = "+total+"/400");
    System.out.println();
    }


    }

    class Markmain
    {
    public static void main(String args[])
    {
    int r,x,i,p,c,e,m;
    String nm;
    Scanner sc =new Scanner(System.in);
    System.out.println("Enter number of students = ");
    x=sc.nextInt();
    Marksheet s[] = new Marksheet[x+1];
    for(i=1;i<x+1;i++)
    {
    System.out.println("Enter records for "+i+" student");
    s[i]=new Marksheet();
    System.out.println("Enter roll no = ");
    r=sc.nextInt();
    System.out.println("Enter name = ");
    nm=sc.next();
    System.out.println("Enter Marks in Physics = ");
    p=sc.nextInt();
    System.out.println("Enter Marks in Chemistry = ");
    c=sc.nextInt();
    System.out.println("Enter Marks in English = ");
    e=sc.nextInt();
    System.out.println("Enter Marks in Maths = ");
    m=sc.nextInt();
    s[i].get(r,nm,p,c,e,m);
    }
    for(i=1;i<x+1;i++)
    {
    s[i].show();
    }
    }
    }

    ReplyDelete
  3. CREATE CLASS TO MANAGE EMPLOYEE INFORMATION USING EMPID, EMP NAME, JOB, SALARY

    import java.util.Scanner;

    public class EmployImformation {
    int empid;
    String name;
    float salary;
    void accept()
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the emp id");
    empid =sc.nextInt();
    System.out.println("enter the name");
    name=sc.next();
    System.out.println("enter the Salary");
    salary=sc.nextFloat();
    }
    void display()
    {
    System.out.println("emp id =" + empid);
    System.out.println("emp name =" + name);
    System.out.println("emp salary =" + salary);


    }

    }
    public class EmployImformationMain {

    public static void main(String[] args) {
    EmployImformation obj=new EmployImformation();
    obj.accept();
    obj.display();

    }

    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post