Inheritance Flashcards
Explain inheritance and abstract classes, provide examples
public abstract class Animal{
public abstract void Sound();
public void Eat(){
Console.WriteLine(“This animal is eating”);
}
}
public class Dog : Animal {
public override void MakeSound(){
Console.WriteLine(“barks”);
}
public void Fetch(){
Console.WriteLine(“Dog is fetching”);
}
In summary, an abstract class requires child classes to implement their own implementation. An abstract class can contain abstract methods, which have no implementation (body), and concrete methods, which have an implementation (body), as seen in the example. The child classes implement their own implementation by using override.
Explain the concept of inheritance and provide an example
Inheritance is a key feature in OOP that enables a new class to absorb the members of an existing class. It establishes a parent-child relationship, where the child inherits the characteristics from the parent class. In contrast to the parent class, which is more general, the child class usually implement its own fields and methods, representing a more specialized group of objects. To perform inheritance, we use the : symbol. A class can extend one or multiple classes, creating a hierarchy of classes.
An example:
public class Animal {
//property…
public string Name {get;set;}
//constructor…
public Animal(string name){
Name=name;
}
}
//child class
public class Dog : Animal{
//constructor
public Dog(string name) : base(name) {
}
//methods and properties unique to clas…
public void Bark() {
Console.WriteLink(“vuuf”);
}
}
Provide an example of inheritance using virtual method
public class Animal {
//properties
public string Name {get;set;}
public int Age {get;set;}
//Constructor
public Animal(string name, int age){
Name=name;
Age=age;
}
//Virtual method
public virtual MakeSound(){
Console.WriteLine($”This {Name} makes a sound”);
}
}
public class Dog : Animal{
//constructor
public Dog(string name, int age) : base(name, age){
}
public override MakeSound(){
Console.WriteLine($”{Name} barks”);
}
}
The parent class - Animal - has a virtual method, that allows its child classes - Dog - to override and provide its own implementation unique to that class. The same result could be achieved with an abstract method, that would require child classes - Dog - to implement the method, and it would also require the parent class - Animal - to be abstract.
Explain ToString() and provide an example
ToString() is a method that converts an object into a string representation. It is useful for displaying information in a particular formatted way. It is often overridden in the child classes to provide a custom string representation.
Example:
public class Person{
public string FirstName {get;set}
public string LastName {get;set}
public override string ToString(){
return $”My name is {FirstName} {LastName}”;
}
}
Explain the concept of polymorphism and how it is related to inheritance
Polymorphism allows us to treat objects from different classes as if they were objects of the same class. The key idea of polymorphism is that each object will “do the right thing” in response to the same method call. So, classes may have different implementations of the same method, but the program can treat them all generically.
Polymorphism is closely tied to inheritance. Polymorphism allows for objects of child classes to be treated as objects of the parent class, which enables flexibility and extensibility within the program.
Polymorphism can also be achieved through interfaces, where objects of different classes can be treated as objects of the same type.
Explain polymorphism and provide an example
Polymorphism allows us to treat objects from different classes as if they were objects of the same class. The key idea of polymorphism is that each object will “do the right thing” in response to the same method call. So, classes may have different implementations of the same method, but the program can treat them all generically.
Example:
public class Vehicle{
public virtual void Move(){
Console.WriteLine(“This vehicle is moving”);
}
}
public class Car : Vehicle {
public override void Move(){
Console.WriteLine(“This car is driving”);
}
}
public class Bicycle : Vehicle {
public override void Move(){
Console.WriteLine(“This bicycle is pedaling”);
}
}
class Program{
static void Main(){
Car car1 = new Car();
Bicycle bicycle1 = new Bicycle();
Vehicle[] vehicles = {car, bicycle};
foreach(Vehicles vehicle in vehicles){
vehicle.Move(); //This is polymorphic behavior!
}
}
}
For example, polymorphism enables us to make an array of vehicles and store both cars, bicycles, and boats, since objects (cars, bicycles, and boats) can be identified by more than one type. This enables us to treat those objects - cars, bicycles, and boats as if they were similar. In this way, we can call a method, and the program will invoke the appropriate method for each type, even though they are stored in the same array.
Why is polymorphism smart?
Polymorphism enables us to treat objects as they were the same type. It makes it possible to make an array of, for example, vehicles that both store cars, bicycles, and boats, since objects (cars, bicycles, and boats) can be identified by more than one type. This enables us to treat those objects - cars, bicycles, and boats as if they were similar. In this way, we can call a method, and the program will invoke the appropriate method for each type, even though they are stored in the same array.
Does polymorphism make sense without inheritance?
Polymorphism can be achieved without inheritance and by using interfaces. Polymorphism can also be achieved through interfaces, where objects of different classes can be treated as objects of the same type.