php – abstract class interview question for fresher & experience

This is very hot burning question , when you attend interview. This is also fact that, the concept of abstract class in oops is very important. so i am going to explain about abstract class, how play important roles in object oriented programming structure.

 

This is following topics , That we will discuss –

 

a >  What is abstract class

b >  What is abstract methods

 

As name  It is clear that , Something hidden in class. We can not directly initialize the class  and we cant not directly create the objects of abstract class.

Abstract class act as parent class , Where we can use abstract class in class hierarchically models. Abstract class abstract methods , which only declare not define .

 

Note –  Many people say , Abstract class is a class , which contain at least one abstract methods. Suppose , you have class , which is contain one abstract methods , then your class is abstract class. It is not right answer.

 

Methods of abstract class , If it is not define in base then it is defines in the child class and methods access specifier should be public or protected in abstract class.

 

example  –

 

abstract class test
{
public function alexawork()
{
return 1;
}
}
$alxa = new test();//throw error in php

 

As above code syntax –   We create the object of abstract class  , so we can not create the object of abstract class , otherwise it will throw error.

Solution – We can create the objects only for child class.

 

Now, We are going to use of abstract class . I think , The following syntax for abstract class will clear your complete concepts –

 

abstract class alexawork
{
public function develop()
{
//code
}
}
class childalexawork extends alexawork
{
public function designer()
{
// code
}
}
$freelnce = new childalexawork();

 

 

Now, from the above code syntax , It is very clear that abstract class is what for that.

 

We have parent class  alexawork . Now we extended this class from child class that is childalexawork  and we create the objects for childs class. 

Use of abstract class is completely to restrict the parent class to initialize or creating objects directly and abstract class is available for inheritance .

 

b >  What is abstract methods –

 

Abstract methods is the methods , which is only declare  in parent class and declare it in child or extended class .

Example –

// declaration

abstract class test
{
public function  abstract alexawork();
}

defined –

abstract class alexawork
{
public function  abstract alexawork();
}

class childalexawork extends alexawork
{
public function  abstract alexawork()
{
// code
}

}

 

Note – abstract methods access only public or protected not private .

Interface class is  the pure example for abstract methods.

 

Thank  alexawork.com  !

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

You may also like...

Leave a Reply

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