Oops With C# Example (what , How , Where)
1.Polymorphism
Method Overloading
Method overriding
2.Abstraction
2.2
package net.javatutorial;
public class Contractor extends Employee {
private int workingHours;
public Contractor(String name, int paymentPerHour, int workingHours) {
super(name, paymentPerHour);
this.workingHours = workingHours;
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * workingHours;
}
}
public class Contractor extends Employee {
private int workingHours;
public Contractor(String name, int paymentPerHour, int workingHours) {
super(name, paymentPerHour);
this.workingHours = workingHours;
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * workingHours;
}
}
2.3
public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String name, int paymentPerHour) {
super(name, paymentPerHour);
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * 8;
}
}
public FullTimeEmployee(String name, int paymentPerHour) {
super(name, paymentPerHour);
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * 8;
}
}
Constructer
A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields:
class Car
{
public string model; // Create a field
// Create a class constructor for the Car class
public Car()
{
model = "Mustang"; // Set the initial value for model
}
static void Main(string[] args)
{
Car Ford = new Car(); // Create an object of the Car Class (this will call the constructor)
Console.WriteLine(Ford.model); // Print the value of model
}
}
Constructor Parameters
class Car
{
public string model;
// Create a class constructor with a parameter
public Car(string modelName)
{
model = modelName;
}
static void Main(string[] args)
{
Car Ford = new Car("Mustang");
Console.WriteLine(Ford.model);
}
}
Access Modifiers
Modifier | Description |
---|---|
public | The code is accessible for all classes |
private | The code is only accessible within the same class |
protected | The code is accessible within the same class, or in a class that is inherited from that class. You will learn more about inheritance in a later chapter |
internal | The code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter |
Properties
class Person
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}
Example explained
The
Name
property is associated with the name
field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.
The
get
method returns the value of the variable name
.
The
set
method assigns a value
to the name
variable. The value
keyword represents the value we assign to the property.
Inheritance
Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.
- Multiple Inheritance(Through Interfaces):In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces. In the image below, Class C is derived from interface A and B.
- Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of inheritance. Since C# doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In C#, we can achieve hybrid inheritance only through Interfaces.
Important facts about inheritance in C#
- Default Superclass: Except Object class, which has no superclass, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class.
- Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because C# does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by C#.
- Inheriting Constructors: A subclass inherits all the members (fields, methods) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
- Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has properties(get and set methods) for accessing its private fields, then a subclass can inherit.
Interfaces
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
Multiple interface
_________________________________________________
public interface interfaceA
{
void DrawA();
}
public interface interfaceB
{
void DrawB();
}
class Program
{
static void Main(string[] args)
{
TestC c = new TestC();
c.DrawA();
c.DrawB();
Console.ReadLine();
}
}
class TestA : interfaceA
{
public void DrawA()
{
Console.WriteLine("This is Class One");
}
}
class TestB : interfaceB
{
public void DrawB()
{
Console.WriteLine("This is Class Two");
}
}
class TestC : interfaceA, interfaceB
{
TestA a = new TestA();
TestB b = new TestB();
public void DrawA()
{
a.DrawA();
}
public void DrawB()
{
b.DrawB();
}
}
Enum
An
enum
is a special "class" that represents a group of constants (unchangeable/read-only variables).
class TestEmployee
{
public enum Month { jan,feb,mar,apr,may}
public static void Main(string[] args)
{
int jan1, feb1, mar1;
jan1 = (int)Month.jan;
feb1 = (int)Month.feb;
mar1 = (int)Month.mar;
Console.WriteLine(jan1);
Console.WriteLine(feb1);
Console.WriteLine(mar1);
Console.ReadLine();
}
}
Comments
Post a Comment