Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Saturday, 2 May 2020

Ques: Abstract class without constructor in C#?

The compiler provides a "Protected Parameterless Constructor" for an abstract class C# program. We know that in an inheritance hierarchy if we create an object of a child class, first base class and then child class constructor will be called.  
Hence, if the base class does not contain any constructor, the compiler provides protect parameterless constructor.

Ques: Is a C# class without constructor possible?

Yes, C# class without constructor is possible. 
We can have a class without any constructor in C#. If we don’t declare any constructors in a class, the compiler automatically provides a public parameterless constructor. And it is ok to instantiate the class without a constructor.
In the below C# code example program, we don’t have any constructor in the class Person.
class Person
{  
    //This class has no constructor
    public void getPersonProfile()
    {
        Console.WriteLine("Base class:getPersonProfile() have no constructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Create object of person
        Person person = new Person();
        person.getPersonProfile();
    }
}

Ques: Can we call a static method like instance method in C#?

NO

If you call the static method using an object the compiler will complain.

Ques: Can we create object of static class in c#?

NO

Ques: How to achieve Abstraction in C#?

Abstraction : 

The concept of “providing necessary details to the clients/users and hiding the unnecessary details” is called Abstraction


As an example consider "A login page where username and password are enough for the users. so, only username and password fields are shown in the login page and validation and database connection should be hidden from the users or clients."
Let’s understand the concept of abstraction by implementing the above scenario with example programs.
There is a class LoginPage that will allow client class or main() program to create an object of its own with username and password information and submit() method only and rest of information e.g. internal methods like VerifyCredential() and DBConnection class will be abstracted from client class.
Below is the C# program to implement the abstraction concept.
using System;
 //Login window class where 
    class LoginPage 
    {
        /*--------------Internal purpose only-----------
  * Internal private methods variable and classes
  * Not visible or accessible out side of login Page
  * class.
  * 
  */
        private String userName;
        private String password;
        private DBConnection connection;

        private void VerifyCredential()
        {

            this.connection = new DBConnection();
            this.connection.DBConnect();
            this.connection.verifyUser(this.userName, this.password);

        }
        /*-------------------------------------------------
  * Only methods visible to User/Client program.
  * Client/User can be main() program or other classes
  */
        public LoginPage(String userId, String pwd)
        {
            this.userName= userName;
            this.password= password;
        }

        public void submit()
        {
            VerifyCredential();
        }     

              
        private class DBConnection
        {

            public void DBConnect()
            {

                Console.WriteLine("Connecting to Database...");

            }

            public void verifyUser(String UserId, String password)
            {
                Console.WriteLine("Verifing the user...");


            }

        }
    }

    /*------------------------------------------
 * Client class or main function  
 */
    public class AbstractionDemo
    {

        public static void Main(String[] args)
        {

            // Put user id and password to login
            LoginPage loginPage = new LoginPage("User1", "pwd123");
            loginPage.submit();

        }

    }
In the above abstraction concept, we have provided only required information to main() program and have hidden the information e.g. internal methods and classes.
To hide the complexities we have adapted some mechanism in above program e.g. we have hide username, password variable, database class, and VerifyCredential() methods by making them private using private modifiers
The way of hiding complexities/information is known as encapsulation. In oops whatever way you can apply to hide information, all are known as The way of hiding complexities/information is known as encapsulation. In oops whatever way you can apply to hide information, all are known as Encapsulation.
Abstraction in C#: Provide only necessary information to the client.
Encapsulation: Whatever the way we apply to hide information is encapsulation.
“In fact, the implementation of encapsulation is Abstraction”