views:

21

answers:

2

Objects with the same attributes and methods belongs to the same class?

  1. Can't I declare two identical classes with the same methods and attributes, instanciate them and have "objects with the same attributes and methods belonging to different classes"?

  2. Can't I declare a class A and a sub-class B (children of the class A) both with the same methods and attributes (and don't declare any new attribute or method on the class B), instanciate them and have "objects with the same attributes and methods belonging to different classes"?

This question is not about good practices... It's about the logical value (true or false) of the question on the title.

+1  A: 

Thats definitely possible.

class A
{

    public string Property1{get;set;}
    public string Method1(int value)
    {
      ......
    }

}

class B
{
   public string Property1{get;set;}
   public string Method1(int value)
   {
     ........
   }
}

Both classes are identical. Have the same properties and methods. But their instances will be different.

Yogendra
+1  A: 

You don't specify what language(s) you're talking about; perhaps there's some exotic language that has the property you describe, but at least in languages like Java and C++ there's no concept of inferring that two classes are "the same" based on what methods/instance variables they declare.

So, the answer is "no, they are not the same class, even though they look the same".

The sub-class case is a little different: if you declare B as a subclass of A (without adding any methods or variables), they are still different classes, but an object of class B also "is-a" A, because of normal inheritance rules.

David Gelhar