To be honest, I took a informational interview this morning.
Basically, I could answer most of questions properly. But, I could not answer a easy question because I was a little nervous. I made Big Big mistake……………………….
What is inheritance?
Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.
What is Polymorphism?
Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors. You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java. Base class object’s pointer can invoke methods in derived class objects. You can also achieve polymorphism in C++ by function overloading and operator overloading.
Inheritance is a common concept. I am a human, except when I first wake up. I inherit certain properties from the class Human, such as my ability to converse more or less and my dependence on air, food, and carbohydrate-based beverages with lots of caffeine. The class Human inherits its dependencies on air, water, and nourishment from the class Mammal, Which inherits from the class Animal.
The ability to pass down properties is a powerful one. It enables you to describe things in an economical way. For example, if my son asks, "What’s a duck?" I can say, "It’s a bird that goes quack." Despite what you may think, that answer conveys a considerable amount of information. My son knows what a bird is, and now he knows all those same things about a duck pus the duck’s additional property of "quackness."
Please see the following sample code.
using System;
using System.Collections.Generic;
using System.Text;
namespace Car
{
class Car {
public int totalDistance;
public int gas;
public int Run(int distacen){
gas = gas – distacen;
totalDistance = totalDistance + distacen;
return totalDistance;
}
public void Fillup(){
gas = 60;
}
}
class Porsche : Car{
public string GetCarType(){
return "Porsche";
}
}
class BMW : Car{
public string GetCarType(){
return "BMW";
}
}
class CarApp{
static void Main(){
Porsche porsche = new Porsche();
BMW bmw = new BMW();
porsche.Run(20);
bmw.Run(30);
Console.WriteLine(porsche.GetCarType() + ": Distance = " + porsche.totalDistance.ToString());
Console.WriteLine(bmw.GetCarType() + ": Distance = " + bmw.totalDistance.ToString());
Console.ReadLine();
}
}
}