Click here to Skip to main content
14,739,223 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is encapsulation and abstraction ?

ans:
print int Id; -> is encapsulation or abstraction 
public int GetId -> abstraction or encapsulation 
{
return Id;
}


This is not encapsulation.means finally we are allowing to access its private members using public member functions, but how do we achieve it?
Posted
Updated 29-May-12 5:10am
v2
Comments
Sergey Alexandrovich Kryukov 29-May-12 12:17pm
   
Makes no sense at all.

When asking questions, you should try to avoid using questionable assumption which could be a result of your preoccupation with some wrong idea. For example, asking a question "what is it, A or B" assumes that A and B are mutually exclusive. Who told you those to concepts are like that? Who told you that a concept can be mapped to a single line of code or a single method? The whole idea to think this way is at least strange...

--SA

The attempt to get mapping between those two concepts and code lines is totally pointless; and an attempt to classify concepts into encapsulation and abstraction is just wrong. Who told you they could be separable? Roughly speaking, abstraction is one of the most universal categories in the whole human culture, mathematics, computer science, philosophy and even art; in computing, it is everywhere. And you are trying to apply the operator "or", totally incorrectly.

For example, Id is an abstraction as the location of any possible integer values, and int is an abstraction as the type which could be a common type of many possible variables.

As to the encapsulation, this term is usually used in the domain of object-oriented programming, but you did not even depict any class (and your syntax for a method is wrong, would not compile). Again, "encapsulation" cannot be a property of any given line of a method; this is a concept. But yes, one concept of encapsulation is the ability to access a private member indirectly, but only indirectly. There is a lot more involved in this concept, so this question makes no sense at all.

Why wouldn't you simply right about the basic concepts first and try to understand them thoroughly, and in the holistic manner, not trying to reduce essential things to trivial? Please see:
http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29[^],
http://en.wikipedia.org/wiki/Object_oriented_programming#features[^].

—SA
   
well to say it very generally encapsulation is the inherit hierarchy and the access modifiers to the class.

abstraction is used in inherit to have a class you cant have a instance of. its generally used so you have a "base" class with the most important info. also an abstract class MUST be override by an child class.

for instance lets assume we wanna make a system containing informations about customers and workers in a store. then we can make a class like this:

public abstract class Person
{
    public string firstname { get; protected set; }
    public string lastname { get; protected set; }
    public int phonenumber { get; protected set; }

    public Person(string _firstname, string _lastname, int _phonenumber)
    {
        firstname = _firstname;
        lastname = _lastname;
        phonenumber = _phonenumber;
    }
}


note that this class is abstract and can not have its own instance and will also act as our "super-class".

next up we create a customer inherit from "Person" so a customer will have at least the same functionality as a person.

public class Customer : Person
{
    public string customerID { get; protected set; }

    public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
        : base(_firstname, _lastname, _phonenumber)
    {
        customerID = _customerID;
    }
}


note here that we're using the "base" keyword to say we use the "superclass" to have these items.

next we make a employee class also inherit from the person class.

public class Employee : Person
{
    public int worktime { get; protected set; }

    public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
        : base(_firstname, _lastname, _phonenumber)
    {
        worktime = _worktime;
    }
}


now to the magic of polymorphism. you can now in you main method (or a collection/database class) make a list with persons and have both employees and customers in that list as seen below.

static void Main(string[] args)
{
    List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
}


as you can see above we add both a employee and a customer in a Person list and that is basically the good thing about polymorphism.

if we expand our small program to also have some abstract and virtual methods that will be override in the inherit we'll get to a point where we will find really good use of our polymorphism. full sample here below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
            persons.OrderBy(p => p.lastname).ThenBy(p => p.firstname);
            foreach (Person p in persons)
            {
                Console.WriteLine(p.ToString());
            }
            Console.ReadKey();
        }
    }


    public abstract class Person
    {
        public string firstname { get; protected set; }
        public string lastname { get; protected set; }
        public int phonenumber { get; protected set; }

        public Person(string _firstname, string _lastname, int _phonenumber)
        {
            firstname = _firstname;
            lastname = _lastname;
            phonenumber = _phonenumber;
        }

        public abstract override string ToString();
        public virtual string GetCustomerID()
        {
            return null;
        }
    }

    public class Customer : Person
    {
        public string customerID { get; protected set; }

        public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
            : base(_firstname, _lastname, _phonenumber)
        {
            customerID = _customerID;
        }

        public override string ToString()
        {
            return firstname + " " + lastname + " " + phonenumber + " " + customerID;
        }

        public override string GetCustomerID()
        {
            return customerID;
        }
    }

    public class Employee : Person
    {
        public int worktime { get; protected set; }

        public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
            : base(_firstname, _lastname, _phonenumber)
        {
            worktime = _worktime;
        }

        public override string ToString()
        {
            return firstname + " " + lastname + " " + phonenumber + " " + worktime;
        }
    }
}


as you can see we have added a abstract tostring method that MUST be override in the child classes. You can add as many as you want either abstract or virtual. abstract have to be override i all child classes where virtual is not needed to be override but it can.

you can find A LOT more about this all over the internet and here on this site. Also i will recommend you to look at interfaces since they some times also can come in handy (see one of my old questions Parse classes to interface that can inherit[^])

lastly, thanks for reading and i hope you find this useful and sorry for my crappy English (danish is my native language). fell free to ask if you have any questions.

- Jackie

PS. the best way to learn it is to try it out by you self, try to make some simple databases, for example try to make a small library/store selling pc parts etc.
   
v3
Comments
symonsarwar 17-Apr-13 14:42pm
   
thanks for thinking positively
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900