abstract class in oops

abstract class in oops

Abstraction

 Abstraction is a process of hiding the implementation details and showing the user related data. In other word hiding the critical data to use and showing only user related data, for example we are sending an email then type massage and send it doesn’t see how it is work. We do not know internal processing. This technique achieve by abstract class. If we want to make a class is abstract then using abstract keyword before class declaration. In this class have two type of methods

    • Abstract method

    • Non-abstract method

• An abstract class must declare with abstract keyword

• It can have abstract and non-abstract method

• Doesn’t create object of this class

• This class can extended

• Every abstract method must override is derived class

• It have final method

Syntax of declaring an abstract class

abstract class class-name{

 member-variable 1 -----

member-variable 2 -----

member-variable n -----

 return-type fuction_name()

{

}

// abstract method

 abstract return-type function_name(); //

}

Example of abstract class

abstract class Person{

    //variables of person class 

    private String personName;

    private String personeAddress;

    private int personeAge;


    // creating constructor 

    Person(){

        // default constructor

    }

    

    Person(String personName,String personeAddress,int personeAge)

    {

        // parameterized constructor

        this.personName=personName;

        this.personeAddress=personeAddress;

        this.personeAge=personeAge;

    }

    

    // methods of person class 

    public void setPersoneName(String personName)

    {

        this.personName=personName;

    }

    public String getPersonName()

    {

        return this.personName;

    }

    public void setPersoneAddress(String personeAddress)

    {

        this.personeAddress=personeAddress;

    }

    public String getPersonAddress()

    {

        return this.personeAddress;

    }

    public void setPersoneAge(int personeAge)

    {

        this.personeAge=personeAge;

    }

    public int getPersonAge()

    {

        return this.personeAge;

    }

    

    @Override 

    public String toString()

        {

            return this.personName+' is belongs to '+personeAddress+' and age is '+personeAge+' year';

        }

    // declare the abstract methods for datahiding 

    abstract public String getGoingTime();

    abstract public String getReadingTime();

    

}


    //declare a student class an dextende the persone class 

    

    class Student extends Person{

        private String goingTime;

        private String readingTime;

        

        // parameterized constructor

        

        Student(String studentName,String studentAddress, int age){

            super(studentName,studentAddress,age);

        }

        

        Student(String studentName,String studentAddress, int age, String goingTime,String readingTime){

            super(studentName,studentAddress,age);

            this.goingTime=goingTime;

            this.readingTime=readingTime;

        }

        

        @Override

        public String getGoingTime()

        {

            if(goingTime==null)

                return 'This student does't study.';

            return 'This student going daily for study at '+goingTime+' AM';

        }

        @Override

        public String getReadingTime()

        {

            if(readingTime==null)

                return 'This student does't reading books ';

            return 'This student daily reading for '+readingTime+' Hours';

        }

        

    }

    

    //declare a NormalMan class an dextende the persone class 

    

    class NormalMan extends Person{

        private String goingTime;

        private String readingTime;

        

        // parameterized constructor

        

        NormalMan(String studentName,String studentAddress, int age){

            super(studentName,studentAddress,age);

        }

        

        NormalMan(String studentName,String studentAddress, int age, String goingTime,String readingTime){

            super(studentName,studentAddress,age);

            this.goingTime=goingTime;

            this.readingTime=readingTime;

        }

        

        @Override

        public String getGoingTime()

        {

            if(goingTime==null)

            return 'This person does't go office.';

            return 'This person daily go office at '+goingTime+' AM';

        }

        @Override

        public String getReadingTime()

        {

                if(readingTime==null)

                return 'This perone is not reading a books. ';

            return 'This person reading books for '+readingTime+' Hours ';

        }

    }


public class Main

{

  public static void main (String[]args)

  {

    Person student=new Student('Ravi vishwakarma','Babhaniyav',22);

    System.out.println(student);

    System.out.println(student.getReadingTime());

    System.out.println(student.getGoingTime()+'\n');

    

    Person student1=new Student('Ravi sankar','Agra',18,'8:00','4');

    System.out.println(student1);

    System.out.println(student1.getReadingTime());

    System.out.println(student1.getGoingTime()+'\n');

    

    Person man=new NormalMan('Ravi vishwakarma','Babhaniyav',22);

    System.out.println(man);

    System.out.println(man.getReadingTime());

    System.out.println(man.getGoingTime()+'\n');

    

    Person man1=new NormalMan('Ravi sankar','Agra',18,'8:00','4');

    System.out.println(man1);

    System.out.println(man1.getReadingTime());

    System.out.println(man1.getGoingTime()+'\n');

  }

}


Last updated:7/1/2021 11:50:35 PM
Ravi Vishwakarma

Ravi Vishwakarma

Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I am pursuing MCA final year from Veer Bahadur Singh Purvanchal University Jaunpur.

0 Comments

Leave Comment