This article discusses the principles of Object Oriented Programming with C# and how to implement them. It includes a sample code for a Rectangle class and a driver class. The article also explains the concepts of inheritance and polymorphism.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Running head: OBJECT ORIENTED PROGRAMMING WITH C# Object Oriented Programming with C# Name of the student: Name of the University: Author note:
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
1 OBJECT ORIENTED PROGRAMMING WITH C# Table of Contents Task 1...............................................................................................................................................2 Task 2...............................................................................................................................................3 Rectangle Class (Rectangle.cs)....................................................................................................3 Driver class (Calculate.cs)...........................................................................................................3 Output..........................................................................................................................................4
2 OBJECT ORIENTED PROGRAMMING WITH C# Task 1 Two main principles of Object Oriented Programming are: ï‚·Inheritance:This is the process by which classes within the same package can inherit certain characteristics and behaviors from leading or existing classes. It is the child class that is known to inherit the accessible properties from the parent class, also known as the rootclass.Theconceptofinheritancehelpstoconceptualizethehierarchical classification of classes and objects. Though inheritance, programmers can manage to re- use various sections of the code. This helps in the reducing space complexity of programs, as the redundant information and functionalities can be inherited from the existing classes. These data or method members are initialized once in the parent classes and are then used by the instances of the child classes. The accessibility of these members in the child classes depends on the access specifiers of these members. Public data members can be accesses by all classes of the package, protected members can only be accessed by the child classes and private members cannot be accessed outside the same class (Smith, 2015). ï‚·Polymorphism: Polymorphism is the term that is used to describe the quality of an element that allows it to exist in more than one form. In object oriented programming, polymorphism refers to the ability via which more than one method are able to exist with the same name but performing a different set of actions. This is done through passing a varietyofparameterorargumenttypesandnumbers.Therearetwotypesof polymorphism namely, static polymorphism and dynamic polymorphism. Method or constructor overloading is the form of static polymorphism. Through this, multiple method instances can be created to perform a variety of operations however they contain
3 OBJECT ORIENTED PROGRAMMING WITH C# the same name. The compiler calls the right method or constructor based on the parameter list that has been passed as the arguments. This is also known as Compile-time polymorphism.Methodoverridingonetheotherhandisanexampleofdynamic polymorphism where the methods to be called are determined at run-time. This is applicable when inherited classes try to reinitialize and use methods of the same name as that of their parent class (Mezini, 2013). Task 2 Rectangle Class (Rectangle.cs) usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceConsoleApp1 { classRectangle { privatedoubleheight; privatedoublewidth; publicRectangle(doubleheight,doublewidth) { this.height= height; this.width= width; } publicdoubleCalcArea() { returnheight* width; } } } Driver class (Calculate.cs) usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser