


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Power of object based languages is that by grouping data and functionality in a single user defined type (UDT).
pay,employee ID.In addition to that we may have a method.
a.GiveBonus(); //increase individual current pay
b.DisplayStats(); //print the information of an employee
class.
class Emp { private string fullname; //private data field private int empID; private float currpay; public Emp(){} //default constructor
Employee attributes: string fullname; int empID; float currpay;
methods: void GiveBonus(float amt); void DisplayStats();
public Emp(string fullname,int empID,float currpay) //overloaded constructor { this.fullName=fullName; this.empID=empID; this.currpay=currpay; } public void GiveBonus(float amt) { currpay+=amt; } public virtual void DisplayStats() { Console.WriteLine(“Name:{0}”,fullName); Console.WriteLine(“pay:{0}”,currpay); Console.WriteLine(“ID:{0}”,empID); } } class Fun { public static void Main() { Employee e=new Employee(“charan”,18,30000); e.GiveBonus(999); Employee e2=new Employee(“cherry”,21,36000); e2.GiveBonus(999); e.DisplayStats(); e2.DisplayStats(); } }
When a class has a set of identically named members, methods that differ by the number(or type) of parameters then we say that methods are overloaded.
class Fun { int x,y; public void setxy(int a) { x=y=a; } public void setxy(int a,int b) { x=a; y=b; }
this.x=x; this.y=y; } public void getxy() { Console.WriteLine(“x={0}”,x); Console.WriteLine(“y={0}”,y); } } class AD { public static void Main() { Fun f1=new Fun(); f1.setxy(100,200); f1.getxy(); } } Output: x= y=
Once you have established a class internal state data and constructor set,your next step is to flesh out the details of the public interface to the class.The term refers to the set of members that are directly accessible from an object variable via the dot operator.
Public interface is declared in a class using ‘public’ keyword.Public interface of a class may be populated by numerous members including the following:
a.Methods:-Named units of work that model some behaviour of a class. b.Properties:-Traditional accessor and mutator functions in disguise. c.Constants/Read-only fields:-Field data that can not be changed after assingment.
class FUN { private int x,y; public void setxy(int a,int b) { x=a; y=b; }
public void getxy() { Console.WriteLine(“x={0}”,x); Console.WriteLine(“y={0}”,y); } } class ABC { public static void Main() { FUN f1=new FUN(); f1.setxy(10,20); f1.getxy(); FUN f2=new FUN(); f2.setxy(100,200); f2.getxy(); } }
Output: x= y=
x= y=
Consider the following class: class FUN //Note lack of access modifier on the class definition. {
} public class FUN //Public types may be created by any other object within the same { as assembly well as by other external assemblies.
} internal class FUN //Internal classes can only be used by other types within the same { assembly.
}
Question:-What are the 3 pillars of OOP? Differentiate b/w “is-a” and “has-a” relation with appropriate program.(14 marks)
Question:-What is encapsulation? What are the 2 ways of enforcing encapsulation? Give examples for both methods.(8 marks)
Question:-What are the 3 pillars of OOP? Differentiate b/w “is-a” and “has-a” relation.What is the difference b/w enforcing encapsulation using traditional accessor and mutators as well as class properties? Explain with necessary code.(12 marks)
a.Encapsulation:-How well does this language hide an object’s internal implementation? b.Inheritance:-How does this language promote code reuse? c.Polymorphism:-How does this language let you treat related objects in a similar way?
Encapsulation is the ability to hide unnecessary implementation details from the object user. Example:- Assume you are using a class named DatabaseReader that has two methods named Open() and Close().
DatabaseReader d=new DatabaseReader(); d.Open(@ “C:\EMP.mdf”); //do something d.Close();
DatabaseReader class has encapsulated the inner details of locating,loading,manipulating and closing the data file.
Encapsulation provides a way to preserve integrity of state data rather than defining public field,define it as private ,which are indirectly manipulated by the caller using one of two main techniques
a.Define a pair of traditional accessor and mutator methods. b.Define a named property.
Two ways of enforcing encapsulation are
a.Using traditional accessor and mutators. b.Using class properties.
Enforcing Encapsulation using traditional accessors and mutators
If you want the outside world to interact with your private data field,we can use accessor(get method) and mutator(set method).
Example:-If you want the outside world to interact with tour private fullName datafield,define accessor and mutator for that field.(Note:-Accessors and mutators are user defined function which sets and returns the value of private data).
public
{
string fullName;
public string GetFullName() {return fullName;} //Accessor
public void SetFullName(string n) //Mutator
{
fullName=n;
}
}
class FUN { public static void Main() { EMP e=new EMP(); e.SetFullName(“gurucharan”); Console.WriteLine(“Name={0}”,e.GetFullName()); e.fullName; //Error cant access private data } }
private
class FUN { public static void Main() { EMP e=new EMP(); e.ID=18; //calling public int ID & Set empID e.Name=aaa; //calling public string Name & Set fullName e.pay=35000; //calling public float pay & Set currpay Console.WriteLine(“EMPID={0}”,e.ID); //here calling public int ID but we have not passed
any value.Therefore it returns the value. Console.WriteLine(“EMP-NAME={0}”,e.Name); Console.WriteLine(“pay={0}”,e.pay); } } NOTE:-“value” is a contextual keyword that represents the implicit parameter used during a property assignment.
Many programmers tend to design traditional accessor and mutator using “get_” and “set_” prefixes.This naming convention itself is not problematic but C# property is internally represented using these same prefix.
Example:
public int ID { get {return empID;} //Maps to get_ID() set {empID=value;} //Maps to set_ID()
public class EMP { public int empID; public int ID { get {return empID;} //changes to get_ID() set {empID=value;} //changes to set_ID() } public int get_ID() {return empID;} //Error already defined public void set_ID(int value) //Error already defined { empID=value; } }
Note:-Constructors must be used. `
Example for read-only property:
public class EMP { private string fullName; private int empID; public EMP(string FN,int eid) { fullName=FN; empID=eid; } public int ID //read-only property { get {return empID;} } } Example for write-only property: public class EMP { private string fullName; private int empID; public EMP(string FN) { fullName=FN; } public int ID //write-only property { set {empID=value;} } }
WKT static members are bound to a given class,not an instance(object) of that class.
Example:
public class EMP { private static int empID; public static int ID { get {return empID;} set {empID=value;} } } public class FUN { public static void Main() { EMP.ID=100; //ID is a static property,it is invoked with class name Console.WriteLine(“empID={0}”,EMP.ID); } }
Closely related to read only properties is the notation of read-only fields.Read-only fields offer data preservation via the “readonly” keyword.
Example:
Assume you have a readonly field named empID that offers an alternative names foe the caller to obtain an employee’s empID.
public class EMP { public readonly int empid; private string fullName; public EMP(string FN,int eid) {
Classical inheritance may be implemented in different combinations.
a.Single inheritance
b.Multilevel inheritance
c.Hierarchial inheritance
A B A B C A
B C D
d.Multiple inheritance(C# does not support it)
Example:
public class EMP { private string empName; private int empID; public string NAME { get {return empName;} set {empName=value;} }
public int ID { get {return empID;}
A B
C
private string empName; private int empID; public EMP(string name,int Id) { empName=name; empID=Id; } } public class Manager:EMP { private int numberofoptions; public Manager(string name,int Id,int numb):base(name,Id) { Numberofoptions=numb; } } public class SalesPerson:EMP { private int numberofsales; public SalesPerson(string name,int Id,int nos):base(name,Id) { Numberofsales=nos; } } public class FUN { public static void Main() { Manager M=new Manager(“aaa”,35,99); SalesPerson S=new SalesPerson(“bbb”,18,20); } }
Suppose consider the constructor of Manager class
public Manager(string Name,int Id,int numb):base(name,Id)
{
Numberofoptions=numb;
}
Suppose if we don’t use “base” keyword then its not possible for us to initialize the data fields of base class. If the access specifier of data fields is public or protected then it can be initialized using derived class constructor as shown below.
public Manager(string Name,int Id,int numb) { empName=Name; empID=Id; numberofoptions=numb; }
C# doesn’t support multiple inheritance, but allows a given type to implement any number of discrete interfaces.
WKT public items are directly accessible from anywhere while private items cannot be accessed from any object beyond the class that has defined it.
C# also defines another access specifier : protected.
When a base class defines protected data or protected numbers, it is able to create a set of items that can be accessed directly by any descendent (child class).
Example:
class A { private int x; protected int y; public int z; } class B:A { public void Setxyz() { x=10; //Error y=20; //OK z=30; //OK } }