Question

write code to demonstrate the features of oop use C# programming.

write code to demonstrate the features of oop use C# programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

OOPS Concepts

Class: A class is made up of three things :- Name, operations, attributes

Object: combination of variables, functions, and data that performs a set of related activities

Encapsulation: when a group of related methods, properties, and other members are treated as a single object.

Inheritance: the ability to receive (inherit) methods and properties from an existing class.

Polymorphism : when each class implements the same methods in varying ways, but you can still have several classes that can be utilized interchangeably.

Abstraction : process by which a developer/coder hides everything other than the relevant data about an object in order to simplify and increase efficiency.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Code Snippet for Class

Class Example {


/* Fields,

Variables,

Properties

*/

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code Snippet for Object


Class Example {
/* Fields,

Variables,

Properties

*/

}

/* Syntax to create object for Class Example */

Example exampleObject = new Example();

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Inheritance:- Inheritance allows the child class to reuse the property of the parent class. It increases code reusability.

Code Snippet for Inheritance

public class parentclass{

public parentclassmethod()
{
Console.WriteLine("Parent class method");
}
}

public class childclass : parentclass /* child class inherting parent class */

{

public childclassmethod
{

Console.WriteLine("Child class method");

}

}

Class Test
{

static void Main(string[] args)
{

childclass obj = new childclass(); /* creating child class object to call parent class method */
obj.parentclassmethod();
}

}

}

output

Parent class method


----------------------------------------------------------------------------------------------------------------

Polymorphism :- are of two types

1. Compile Time Polymorphism :- we will declare method with same name but with different parameter/signature

Code snippet

public class ParentClass
{
public void sum(int a, int b) {

Console.WriteLine(a+b);
}

public void sum(int a, int b, int c)
{
Console.WriteLine(a+b+c);
}
}

Class Test
{
static void Main(String[] args)
{

prentclass obj = new ParentClass();
obj.sum(10,20);
obj.sum(10,20,25);
Console.ReadLine();
}
}
}

Output

30
55

2. RunTime Polymorphism:- when we declare a method with same name and same parameter/signature in different classes is called run time polymorphism.
Basically, we override a method in the base class by creating a similar method in the derived class by doing inheritance and by using virtual and override keywords.

Code Snippet

public class ParentClass {

public virtual void Display()
{
Console.WriteLine("Parent Class method");
}

public class childclass : ParentClass /* inheriting parent class *\
  
{   

public override void Display()
  
{
  
Console.WriteLine("Child Class method");

}

}

Class Program

{

static void Main(string[] args)

{

ParentClass obj= new childclass();
obj.Display();
Console.ReadLine();

}
}
}

Output

Child Class method

Add a comment
Know the answer?
Add Answer to:
write code to demonstrate the features of oop use C# programming.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT