Assignment
Assignment
Methods –
1) Write a class. Name it as “Program” class. Follow below steps.
a) Write below methods
Public Void Print() – it will print some message.
Public int Add(int a, int b) – it should add 2 values and return sum.
Public int Sub(int a, int b) – it should subs 2 values and return subs.
Public int Div(int a, int b) – it should divide 2 values and return div.
Public int Mult(int a, int b) – it should Mult 2 values and return Mult.
b) Create an object of the Program class in the Main() method.
c) Call above method using an object and print an output for each method with some meaning
full message.
Access Modifiers-
1) Write a class. Name it as “Student” class. Follow below steps.
2) Define below variables
Public int I=1;
Private int j=1;
Protected int k =1;
3) Create an object of the “Student” class in the Program class in Main() method
4) Access below variables using an object and print an output. Also observer behavior of the
variable access. Notify and analyze if any error occurs.
Properties-
Public int Sub(int a, int b) – it should subs 2 values and return subs.
Public int Sub(int a, int b, int c) – it should subs 3 values and return subs.
Public int Sub(int a, int b, int c, int d) – it should subs 4 values and return subs.
Public int Div(int a, int b) – it should divide 2 values and return div.
Public int Div(int a, int b, int c) – it should divide 3 values and return div.
Public int Div(int a, int b, int c, int d) – it should divide 4 values and return div.
Public int Mult(int a, int b) – it should Mult 2 values and return Mult.
Public int Mult(int a, int b, int c) – it should Mult 3 values and return Mult.
Public int Mult(int a, int b, int c, int d) – it should Mult 4 values and return Mult.
d) Create an object of the “Maths” class in Program class in the Main() method.
e) Call above method using an object and print an output for each method with some meaning
full message.
Constructor Overloading-
Inheritance –
Single Inheritance -
Multi-Level Inheritance -
Hierarchical Inheritance -
3) Write and program where define BaseClass and inherit this in the program class. Define default
parameterized constructor in the BaseClass. In the Program class use “base” keyword to call
base class default and parameterized constructor. Create an object of the program class in main
method and write execution and output.
4) Write a program as given in the below screen shot. Compil,run and debug the program and
write an output.
5) Write BaseClass with sealed keyword and inherit this in the program class. Compile the program
and write an output.
6) What is an object class? Write methods provided by it.
7) Create an array of object class with size 5. Add diff datatype of values in the array and print
values from the created array.
8) Define 1 Base class and inherit this in the program class. Define Public Void Draw() method in
the Base and program class with same definition. Write warning message in the assignment file.
9) Define 1 Base class and inherit this in the program class. Define Public Void Draw() method in
the Base and program class with same definition only use “new” keyword in the program class
method. Compile the program and check if any warning show there.
10) Write a program as given below. Compile and debug the program and write output and
obeservatoins.
11) Write a program as given below in the screen shot. Write some meaning full message in every
method method/function given below. Write and output. Also debug the program and write
observations.
12) What is method overriding? And how to do it. Explain with an example.
13) What is virtual and override keyword?
14) Write below program. Compile and run the program and write output. Also debug the program
write obeservations.
15) Write a program as given in the below screen shot.
Description -
1) abstract class defined with abstract keyword
2) cannot create an instance of abstract class
3) Must inherit to sub call to use functionality
4) abstract class can have abstract as well as not abstract members(prop/method)
5) abstract method/prop must override in derived class
6) abstract method/prod does not have a body in abstract class
7) syntax
8) example
9) cannot inherit multiple classed in one class
10) can be used access modifiers
11) can be used static method/member
12) static member can be defined in the abstract class
13) cannot define static abstract method
14) abstract class cannot be sealed.
Questions
2) Define an Abstract class named AbsBaseClass. Inherit this in the program class. In the main
method create an object of the abstract class. Compile and run the program and write an
output.?
3) Define an Abstract class named AbsBaseClass1. Inherit this in the program class. In the
AbsBaseClass1 class define abstract method: AbsMethod(). Compile and run the program and
write an output.?
4) Define an Abstract class named AbsBaseClass2. Inherit this in the program class. In the
AbsBaseClass2 class define instance(normal) method: InsMethod(). Compile and run the
program and write an output.?
5) Define an Abstract class named AbsBaseClass3. Inherit this in the program class. In the
AbsBaseClass3 class define abstract property: string Name. Compile and run the program and
write an output.?
6) Define an Abstract class named AbsBaseClass4. Inherit this in the program class. In the
AbsBaseClass4 class define instance property: string Name. Compile and run the program and
write an output.?
7) Define an Abstract class named AbsBaseClass5. Inherit this in the program class. In the
AbsBaseClass4 class define abstract method: AbsMethodOver() and one normal/instance
method named: NormalMethod(). Override AbsMethodOver() method in the Program class.
Create an object of the program class and call AbsMethodOver() and NormalMethod() method.
Compile and run the program and write an output.?
8) Define an abstract class with sealed keyword and compile and write an output?
9) Define an abstract class with static keyword and compile and write an output?
10) Define an abstract class with abstract static method named: AbsStaticMethod() and compile and
write an output?
11) Define an abstract class with abstract method named: AbsMethod() and define method body
as well and compile and write an output?
12) Below is the given program with some business logic. Write a below program in c# and compile,
debug and run the program. Write an output and also write detailed explanation.
13) Write a below program in c#. It demonstrates inheritance in Abstract. Compile,debug and write
an output and detailed description.
using System;
namespace AbstractClass {
// non-abstract method
public void display() {
Console.WriteLine("Non abstract method");
}
}
Console.ReadLine();
}
}
}
-------------------------------------------------------------------------------------------
14) Write below program in c#, debug, compile and run the program. It’s Implementation of the
abstract method. Write an output and explanation?
using System;
namespace AbstractClass {
// abstract method
Console.WriteLine("Bark Bark");
class Program {
obj.makeSound();
Console.ReadLine();
-------------------------------------------------------------------------------------------
15) Define Abstract class with get and set accessors? write below program. Compile,debug and run,
using System;
namespace AbstractClass {
// abstract method
get;
set;
return name;
set {
name = value;
class Program {
obj.Name = "Tom";
Console.ReadLine();
16) Define an abstract class to demonstrate constructor. Debug, compile and run the program, write
an output and write and explanation.
using System;
namespace AbstractClass {
abstract class Animal {
public Animal() {
Console.WriteLine("Animal Constructor");
}
}
class Program {
static void Main (string [] args) {
// create an object of Dog class
Dog d1 = new Dog();
Console.ReadLine();
}
}
}
C# Abstraction
The abstract classes are used to achieve abstraction in C#.
The major advantage of hiding the working of the brake is that now the
manufacturer can implement brakes differently for different motorbikes.
However, what brake does will be the same.
17) C# Abstraction – write below program to understand abstraction concept in c# abstract class.
using System;
namespace AbstractClass {
abstract class MotorBike {
class Program {
s1.brake();
m1.brake();
Console.ReadLine();
CH6 – Interfaces
1) Interface defined with interface keyword
2) cannot create an instance of Interface
3) Must implements to sub call to use functionality
4) Interface has all members public and abstract bydefault
5) interface method/prop must override in derived class
6) Interface method/prod does not have a body in Interface
7) syntax
8) example
9) Can implement multiple interfaces in one class
10) cannot use access modifiers inside interface
11) cannot static method/member
12) interface to interface connection called Inheritance
13) interface to class connection called Implementation
14) only prop - indexer - method can be defined in the interface
Assign:
1. interface Dem
2. {
3. void call ();
4. void show ();
5. }
6. class Drived : Dem
7. {
8. public void call()
9. {
10. Console.WriteLine("You are in Call Method");
11. }
12. public void show()
13. {
14. Console.WriteLine("You are in show Method");
15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Drived dr = new Drived();
22. dr.call();
23. dr.show();
24. Console.ReadLine();
25. }}
2) Write a below program. Define interface with the structure. Describe a an output.
1. interface Dem
2. {
3. void call();
4. void show();
5. }
6. struct Str : Dem
7. {
8. public void call()
9. {
10. Console.WriteLine("Call Method of Strcut");
11. }
12.
13. public void show()
14. {
15. Console.WriteLine("Show Method of Struct");
16. }}
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Str st = new Str();
22. st.call();
23. st.show();
24. Console.ReadLine();
25. }}
1. interface Dem
2. {
3. void call();
4. }
1. interface Dem
2. {
3. void call();
4. }
5. interface Dem2
6. {
7. void show();
8. }
9. class Str : Dem,Dem2
10. {
11. public void call()
12. {
13. Console.WriteLine("Call Method of Strcut");
14. }
15. public void show()
16. {
17. Console.WriteLine("Show Method of Struct");
18. }}
19. class Demo
20. {
21. static void Main(string[] args)
22. {
23. Str st = new Str();
24. st.call();
25. st.show();
26. Console.ReadLine();
27. }}
1. interface Intr
2. {
3. void Method1();
4. void Method2();
5. }
6. class Demo2 : Intr
7. {
8. public void Method1()
9. {
10. Console.WriteLine("Method1");
11. }
12. public void Method2()
13. {
14. Console.WriteLine("Method1");
15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Demo2 DmRef = new Demo2();
22. Intr Intrf = DmRef;
23. Intrf.Method1();
24. Intrf.Method2();
25. Console.ReadLine();
26. }}
1. interface Intr
2. {
3. void Method1();
4. void Method2();
5. }
6. class Demo2 : Intr
7. {
8. void Intr.Method1()
9. {
10. Console.WriteLine("Successfully Call Method1");
11. }
12. void Intr.Method2()
13. {
14. Console.WriteLine(" Successfully Call Method2");
15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Demo2 DmRef = new Demo2();
22. Intr Itr = DmRef;
23. Itr.Method1();
24. Itr.Method2();
25. Console.ReadLine();
26. }}
Chapter No 7 -Assignment…