0% found this document useful (0 votes)
19 views

Nheritance: Inheritance Is Specific To Object-Oriented Programming, Where A New Class Is

Inheritance allows new classes to be created from existing classes. The subclass inherits all the attributes and methods of the parent class and can define additional attributes and methods. This allows code reusability and is a fundamental concept in object-oriented programming. Subclasses inherit attributes and behaviors from the superclass but can override or extend them.

Uploaded by

Owais Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Nheritance: Inheritance Is Specific To Object-Oriented Programming, Where A New Class Is

Inheritance allows new classes to be created from existing classes. The subclass inherits all the attributes and methods of the parent class and can define additional attributes and methods. This allows code reusability and is a fundamental concept in object-oriented programming. Subclasses inherit attributes and behaviors from the superclass but can override or extend them.

Uploaded by

Owais Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

INHERITANCE:-

Inheritance is specific to object-oriented programming, where a new class is


created from an existing class. Inheritance (often referred to as subclasses)
comes from the fact that the subclass (the newly created class) contains the
attributes and methods of the parent class. The main advantage of inheritance is
the ability to define new attributes and new methods for the subclass which are
then applied to the inherited attributes and methods.

Example 1:
Classes can have 'Children' that is, one class can be created out of another class. The
original or parent class is known as the Super Class (or base class). The child class is
known as the Sub Class (or derived class).

PRGRAM:-

using System;

public class superclass


{
public void print()
{
Console.WriteLine("I'm a super class .");
}
}

public class subclass : Parent


{
public void cprint()
{
Console.WriteLine("I'm a subclass.");
}
public static void Main()
{
subclass child = new subclass ();
child.print();
}
}
Example 2:

A Sub Class inherits all the attributes and behaviors of the Super Class, and may
have additional attributes and behaviors.

PRGRAM:-

using System;

public class Mammal


{
public virtual void Greet()
{
Console.WriteLine("Hello, I'm some sort of Mammal!");
}
}

public class Dog : Animal


{
public override void Greet()
{
Console.WriteLine("Hello, I'm a dog!");
}
}
public class Wildcats: Animal
{
public override void Greet()
{
Console.WriteLine("Hello, I'm a Wildcats and I can hunt and kill anyone");
}
}

Example 3:

The attributes and operations in the diagram below are for vehicle class, which
the SubClass inherits. Extra operation is the land Vehicle works only on Land
and the water vehicle works only in water.

Next example below is of Multiple Inheritance

You might also like