- Encapsulation (प्रावरण) (સંપુટીકરણ )
- Inheritance (धरोहर) (ધરોહર)
- Polymorphism (बहुरूपता ) (બહુરુપતા)
- Encapsulation
Encapsulation conceals the functional details of a class from objects that send messages to it.
e.g. the Dog class has a bark() method. The code for the bark() method defines exactly how a bark happens (e.g., by inhale() and then exhale(), at a particular pitch and volume). Timmy, Lassie's friend, however, does not need to know exactly how she barks. Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface those members accessible to that class. The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in future, thereby allowing those changes to be made more easily, that is, without changes to clients. For example, an interface can ensure that puppies can only be added to an object of the class Dog by code in that class. Members are often specified as public, protected or private in C#. Because JavaScript is not a statically typed language, it does not provide a keyword for defining a class or object-type definition. Additionally, because JavaScript is not compiled, there would be no way to enforce the proper use of such types. However, it is still possible to define custom objects in JavaScript that behave, in many ways, like classes in C# .
// C# Pet class
public class Pet
{ private string name;
public Pet(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
}
consume the C# code
Pet p = new Pet("Max");
System.Console.WriteLine(p.GetName());
// JavaScript Pet class
function Pet(name)
{
this._name = name;
}
Pet.prototype._name;
Pet.prototype.getName = function()
{
return this._name;
}
consume javascript class
var p = new Pet("Max");
alert(p.getName());
- Inheritance
Inheritance in object-oriented programming allows developers to define an "is a" relationship between classes. For example, we might want to extend our object model to define slightly more specialized versions of the Pet class: Dog and Cat. The base class, Pet, will contain any properties or methods shared by all Pets, but Dog and Cat may define additional properties or methods applicable only to instances of those classes. For example, our Dog class will provide a wag tail method, and the Cat class will provide a purr method.
// C# Dog class
public class Dog : Pet
{
public Dog(string name) : base(name)
{
}
public void WagTail()
{
// Wagging
}
}
// C# Cat class
public class Cat : Pet
{
public Cat(string name) : base(name)
{
}
public void Purr()
{
// Purring
}
}
consume C# code
Dog d = new Dog("Max");
d.WagTail();
Cat c = new Cat("Fluffy");
c.Purr();
// JavaScript Dog class
function Dog(name)
{
Pet.call(this, name);
}
Dog.prototype = new Pet();
Dog.prototype.wagTail = function()
{ // Wagging
}
// JavaScript Cat class
function Cat(name)
{
Pet.call(this, name);
}
Cat.prototype = new Pet();
Cat.prototype.purr = function()
{ // Purring
}
consume javascript code
var d = new Dog("Max");
d.wagTail();
var c = new Cat("Fluffy");
c.purr();
call() is a built-in JavaScript function that is used to invoke a specific target function in the context of a specific object. In this case, we are invoking the Pet constructor function in the context of the Cat or Dog instance. In other words, when Pet() is called, the implicit JavaScript variable will refer to the instance of Cat or Dog that is being constructed.
- Polymorphism
Polymorphism refers to the ability of a caller to invoke a particular method or set of methods on an object without regard to the object's type. For example, we might want to add a speak() method to our Pet class, to allow our Pets to answer the phone when we are not at home; the caller on the other end of the line will not necessarily know or care which Pet is answering the phone, as long as it is able to speak():
// C# Pet class
public class Pet
{
// ...
public virtual void Speak()
{
System.Console.WriteLine(GetName() + " says...");
}
}
// C# Dog class
public class Dog : Pet
{ // ...
public override void Speak()
{
base.Speak();
System.Console.WriteLine("woof");
}
}
// C# Cat class
public class Cat : Pet
{
// ...
public override void Speak()
{
base.Speak();
System.Console.WriteLine("meow");
}
}
Consume C# code
p = new Dog("Max");
p.Speak();
p = new Cat("Fluffy");
p.Speak();
// JavaScript Pet class
// ...
Pet.prototype.speak = function()
{
alert(this.getName() + " says...");
}
// JavaScript Dog class
// ...
Dog.prototype.speak = function()
{
Pet.prototype.speak.call(this);
alert("woof");}
// JavaScript Cat class
// ...
Cat.prototype.speak = function()
{
Pet.prototype.speak.call(this);
alert("meow");
}
Note that the same call() function used to invoke the base class constructor functions is also used to invoke method on the base class.
consume javascript code
p = new Dog("Max");
p.speak();
p = new Cat("Fluffy");
p.speak();