Sie sind auf Seite 1von 5

Inheritance

vardana solution presents awesome C sharp : inheritance

by santosh For more information please visit

http://awesomecsharp.blogspot.in/2013/07/inheritance-in-awesome-c-sharp-by.html
Hi... friends how are u all ? i m good and looking same for u all. Friends we live in the small beautiful world, where we see birds flying in the sky, rivers, mountain isn't it awesome that this all is free, we all have the free gift from nature we should respect that freedom from nature and tries to manage the pollution rate and share happiness to people face. Now Let's move to the c sharp programming, today i m going to post on Inheritance. C sharp is an object oriented programming technology based on oops which implement modular approach and real world programming. In real world a well known concept is inheritance. Friends when any young once takes birth we celebrate that moment by saying that child looks like his/her parents or grandparents. We use to say that nose of the child or eyes of the young one meets to that family member i.e we discuss the genetic trait which is passed over from one generation to the next generation is called inheritance. That concept of inheritance is also implemented in oops programming like c sharp. we have different classes in the programming code meant for different functionality, their functionality can be transmitted from one class to the another so that we can easily manage the programming utilities. Let's Inheritance technically. Friends entity do not exist in isolation, they have some type of relationship with each other.

Here in this code i have created two classes Car and Man. where Man class is the child class and Car is the base class, now Man class (subclass / child class) inherits the Car class. For implementing inheritance C sharp applies : operator. We already know that Car is the base class which have two functions Accept(); and Display(); in which Accept is non static and Display is static can be invoked be initializing the object of the Man class. code is here using System; class Car { public void Accept() { Console.WriteLine("Accept invoked"); } public static void Display() { Console.WriteLine("Display invoked"); } class Man : Car {} class van {} public static void Main() { Man m = new Man(); m.Accept(); Display(); }}

Inheritance concept applies on class attributes and features, you can also inherits the classes variable from base to child. Let's see this also how to inherit the int variable from base to child.

The process is same as we have implemented in the previous code example but here my focus was to share int a variable between two different classes one is base class (Car class) other one is child class Friends you can use any variable like string, char , float, double and so on..... Code is here using System; class Car { int a; class man :Car { public void Display() { Console.WriteLine(++a); } } public static void Main() { new man().Display(); Console.ReadKey(); }}

friends

new man().Display();

is the code which will call Display function which is little bit different from my regular code which

is Car C= new Car(); C.Display(); But it all is same in functionality. In Awesome c sharp programming classes and objects are related to each other. They act and react with other classes i have already shown u that how they do that. This makes programming close to the real world and reduces the load from programming culture and code of ethics. These relations in classes make programmer work easy. you can identify the relationship among the objects and classes.

Kind of relationship
There are various relationship between objects of different classes in an object environment. (1) Inheritance relationship (2) Composition relationship (3) Utilization relationship (4) Instantiation relationship Friends time to see inheritance relationship in more details. Let's see the below picture first

Friends Automobile class is the super class and bus and car are sub classes. friends let us consider the automobile class. An automobile class is a vehicle which has engine, wheels, cabinet etc. These basic features are common in both sub classes like car and bus, but point to note here is bus and car classes are different with each other for example bus size is comparable bigger than car size. but both have some properties of vehicle i.e of automobiles. So here automobile class can play role of Abstract class [friends for abstract class please refer my previous post : abstract ] From here two important technical terms arises Generalization and specialization. Generalization:- Friends Generalization is the collections of similar types of properties, for example consider about an school or university where several students studies in different classes. But they all are same because they are students of that school or university. That common attributes of students can be called as generalize feature. in the same way Abstract classes have generalize features i.e collection of all common attributes which are distributed or shared in child.classes or sub classes.

Specialization :- Friends Again taking same example of school university, this time several students have certain classes. They studies only some selected subjects not all subjects being taught in university. That selective feature is specialization. Composition relationship : Awesome C sharp allows you to form an object, which includes another object as it's part. This mechanism of forming an object is called Composition. For example a class named Car consist of an engine, Therefore Car class is composed of engine. This type of example is know as composition.

utilization Relationship Awesome C sharp allows a class to make use of another class. This kind of relationship is called utilization relationship. For example Honda_City is the object of class honda. And peter is the object of the driver class. But peter boss also has another object (honda_civic) of same Car class [i.e boss have another Honda_ Civic car of Honda class ] in that case peter can drive any of the car that boss want. Boss doesn't need to have another driver for driving another car. .:. peter can drive any of these car i.e utilization relationship. instantiation relationship An instantiation relationship as the name suggest is the relationship between a class and a instance of that class. for example Car C= new Car(); C is object of car class.

Friends we have Seen constructors in my previous post [constructor] nut here i m determining inheritance hierarchies in Constructor

Friends if u are having problem to getting this i suggest u to visit my constructor and destructor post. If a class is inherits another class class, then that base class constructor will be called first and in case case of destructor firstly its own class destructor is called then in base class. Try it by youself

Interface:
Friends c sharp doesn't support multiple inheritance, we have interface in c sharp now let's see interface in detail. Interface defines properties, method and event which are known as member of the interface. It is important to note here that interface contains only declaration of the members. Classes and structure implements these interface members. Interface is used when you want to use standard structure of method codes followed by classes, where classes will implements the functionality. Whereas abstract classes are used in the situations where you want only few methods to declare by the base class and drive class will implement the functionality.

Friends it is good practice to write initial I before interface because it help us to determine that define keyword is an interface in the programming. Interface is defined above of the class and inherited by the classes which will implements interface functions. This interface provides stability and the structure while coding here is code guys using System; public interface IOM { void Accept(); void Display(); } class Car:IOM { public void Accept() { Console.WriteLine("awesome C sharp"); } public void Display() { Console.WriteLine("happy learning "); } public static void Main() { Car C = new Car(); C.Accept(); C.Display(); Console.Read(); } } Dear friends i hope i made u comfortable in Awesome c sharp inheritance. hope u will like it. if u have any suggestion u can mail me at developer.santosh@live.in. Guys don't forget to visit again i m going to post next awesome c sharp post on C sharp Streaming [ input output]. I thanks all of u for your visit. Keep sharing your smile, and always remember that u are the reason of somebody smile and last thing

u r awesome
For more information please visit

see u in next post guys.

http://awesomecsharp.blogspot.in/2013/07/inheritance-in-awesome-c-sharp-

by.html

Das könnte Ihnen auch gefallen