The 3 Pillars of OOP
Instructions
First, you will work through an example of inheritance, encapsulation, and polymorphism together as a class. Then you will complete a second example on your own for added practice.
Materials to turn in:
- Lab06 Project Folder
- Lab06.master
- Lab06.master.cs
- default.aspx
- default.aspx.cs
- dog.aspx
- dog.aspx.cs
- Dog.cs
- Poodle.cs
- phones.aspx
- phones.aspx.cs
- CellPhone.cs
- Motorola.cs
- Razr.cs
- images and other associated project files
Procedures (together as a class)
- Master Pages
- Set some aesthetic properties of the master page
- In design view, you can use the Properties panel to adjust properties.
- Add 4 <div> elements to the master page
- In the first <div>, add an Image control out of the Toolbox.
- In the second <div>, add a navigation Menu out of the Toolbox that points to the two content pages you're going to create (default.aspx, phone.aspx, dog.aspx)
- drag the navigation menu on to the source code view where you want it.
- switch to design view and use the control to add the menu items.
- In the body <div>, add a <span> element. In the span, put the ContentPlaceHolder.
- In the last <div>, add copyright text
- Add a content page (default.aspx) that references your master page for this part. Your output for this part should be written to the ContentPlaceHolder or a control that you place within it.
- Inheritance
- Inheritance allows code reuse by building on existing classes. It is the concept of deriving classes from a base class where the derived class "is a" one of the base class.
- Create a new Class: Dog
- Give it two private strings: name, gender
- In the constructor, initialize the name and gender to some default value
- Create a function: Bark() that returns a string "ruff!"
- Create a new Class: Poodle
- Poodle must inherit from Dog -- Poodle : Dog
- Give it a private string: breed
- In the constructor, initialize the breed to: Poodle
- In default.aspx.cs
- Declare an instance of Poodle: puddle
- In Page_Load(), call puddle.Bark() and write out the value to the browser.
- Notice that Bark() is accessible for the Poodle, even though Bark() is defined in Dog.
- Notice that breed, name, and gender are not accessible because they have been defined using the private access modifier.
- Encapsulation
- Encapsulation is a method of hiding object data and implementation details, but defining properties and methods to let the caller have access to it.
- Create a new public function that returns a string, inside of Poodle: getBreed()
- return the variable using .ToString()
- Create a new public function that returns a string, inside of Dog: getName()
- return the variable using .ToString()
- Create a new public function that accepts a string as a parameter, inside of Dog: setName(string n)
- set the name equal to the string that was passed into the function
- Create a new public function that returns a string, inside of Dog: getGender()
- return the variable using .ToString()
- Create a new public function that accepts a string as a parameter, inside of Dog: setGender(string g)
- set the gender equal to the string that was passed into the function
- In default.aspx.cs
- In Page_Load(), call puddle.getBreed(), getName(), getGender() and write them to the screen (format for legibility).
- Then use setName() and setGender() to change both the name and gender of the Poodle.
- Then call getName() and getGender() again and write them to the screen.
- Polymorphism
- In C#, polymorphism means the ability for classes to share the same methods (actions) but implement them differently.
- Redefine Bark() in Poodle
- public new string Bark() -- return "ruff! ruff!" instead of just "ruff!"
- Notice that you will have to use the keyword new
- Notice when the page is reloaded in the browser that the function inside of Poodle is firing now instead of the function inside of Dog
- Congratulations - you have just accomplished polymorphism.
Procedures (on your own)
- Add a second content page (phones.aspx) that references your master page for this part. Your output for this part should be written to the ContentPlaceHolder or a control that you place within it.
- Inheritance
- Inheritance allows code reuse by building on existing classes. It is the concept of deriving classes from a base class where the derived class "is a" one of the base class.
- Create a new Class: CellPhone
- Give it a private string: phoneType
- In the constructor, initialize the phoneType to "flip phone"
- Create a function: Ring() that returns a string "ring ring! ring ring!"
- Create a new Class: Motorola
- Motorola must inherit from CellPhone -- Motorola : CellPhone
- Give it a private string: network
- In the constructor, initialize network to: "GRSM"
- Create a new Class: Razr
- Razr must inherit from Motorola -- Razr : Motorola
- Give it a private string: phoneCompany
- Give it a private System.Drawing.Color: color
- In the constructor, initialize phoneCompany to: "AT&T", and color to: Blue
- In phones.aspx.cs
- Declare an instance of Motorola: moto
- Declare an instance of Razr: myRazr
- Declare another instance of Razr: yourRazr
- In Page_Load(), instantiate instances of moto, myRazr, and yourRazr
- In Page_Load(), call moto.Ring() and write out the value to the browser.
- In Page_Load(), call myRazr.Ring() and write out the value to the browser.
- In Page_Load(), call yourRazr.Ring() and write out the value to the browser.
- Notice that Ring() is accessible for the Motorola class, even though Ring() is defined in the CellPhone class.
- Also notice that Ring() is accessible for the Razr class, even though Ring() is defined in the CellPhone class.
- Notice that phoneType, network, color and phoneCompany are not accessible from either the Razr class or the Motorola class because they have been defined using the private access modifier.
- Encapsulation
- Encapsulation is a method of hiding object data and implementation details, but defining properties and methods to let the caller have access to it.
- Create a new public function that returns a string, inside of CellPhone: getPhoneType()
- return the variable using .ToString()
- Create a new public function that returns a string, inside of Motorola: getNetwork()
- return the variable using .ToString()
- Create a new public function that returns a string, inside of Razr: getPhoneCompany()
- return the variable using .ToString()
- Create a new public function that accepts a string as a parameter, inside of Razr: setPhoneCompany(string pc)
- set phoneCompany equal to the string that was passed into the function
- Create a new public function that returns a System.Drawing.Color, inside of Razr: getColor()
- return the variable using .ToString()
- Create a new public function that accepts a string as a parameter, inside of Razr: setColor(System.Drawing.Color c)
- set the variable color equal to the color c that was passed into the function
- In phones.aspx.cs
- In Page_Load(), call moto.getPhoneType(), moto.getNetwork(), myRazr.getPhoneCompany(), myRazr.getColor(), yourRazr.getPhoneCompany(), and yourRazr.getColor() -- and write them to the screen (format for legibility).
- Then use myRazr.setPhoneCompany(), myRazr.setColor() to change the phoneCompany to "Verizon" and color to "Red".
- Then call myRazr.getPhoneCompany(), myRazr.getColor(), yourRazr.getPhoneCompany(), and yourRazr.getColor() again and write them to the screen.
- Notice that the values you set are inside the Razr class, however you only changed the values for myRazr and not for yourRazr.
- Polymorphism
- In C#, polymorphism means the ability for classes to share the same methods (actions) but implement them differently.
- Redefine Ring() in Razr
- public new string Ring() -- return "bbbbbbbrrrrrrrrrrrrriiiiiiiiiiiiiiiiiinnnnnnnnggggg"
- Notice that you will have to use the keyword new
- Notice when the page is reloaded in the browser that the function inside of Razr is firing now instead of the function inside of CellPhone
- Notice that when moto.Ring() is called, however; it still says "ring ring! ring ring!"
- Congratulations - you have just accomplished polymorphism.
- Your final output should look fairly similar to this:
-
POODLE
before
arf! arf!
Poodle
female
Sandy
after
male
Sunny
CELL PHONE
moto: ring ring! ring ring!
myRazr: bbbbbbbrrrrrrrrrrrrriiiiiiiiiiiiiiiiiinnnnnnnnggggg
yourRazr: bbbbbbbrrrrrrrrrrrrriiiiiiiiiiiiiiiiiinnnnnnnnggggg
before
moto: flip phone
moto: GRSM
myRazr: AT&T
myRazr: Color [Blue]
yourRazr: AT&T
yourRazr: Color [Blue]
after
myRazr: Verizon
myRazr: Color [Red]
yourRazr: AT&T
yourRazr: Color [Blue]
- Code
- Images
Grading
Does it work |
-3 pts max |
Master Pages |
-5 pts max |
Polymorphism |
-5 pts max |
Encapsulation |
-5 pts max |
Inheritance |
-5 pts max |
Comments |
-2 pts max |
Total |
25 pts possible |
|