Alpha breathing Evocation Composition Aggregation and Inheritance General
Alpha breathing
Evocation
Composition, Aggregation and Inheritance
General Objective �To understand composition, aggregation and inheritance
Specific Objective 1 �To illustrate the association, aggregation and composition
Association � Represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. � They can be created and deleted independently.
Aggregation � It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or apart-of” relationship.
Composition � It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted.
Specific Objective 2 �To illustrate 2 forms of inheritance
Inheritance � The mechanism of constructing one class from another class � Enables reusability of some or all the properties of existing classes � This existing class is called the base class, and the new class is referred to as the derived class. � Can be achieved in 2 forms 1. Classical form 2. Containment form
Classical inheritance � Provides � Types ◦ ◦ “is-a” relationship of inheritance Single inheritance Multiple inheritance Hierarchical inheritance Multilevel inheritance
Containment inheritance � Provides “has-a” relationship class A {. . . } class B { A a; }B b; � Class A depends on class B but not vice versa
Defining a subclass <acess-specifier> class <base_class> {. . . } class <derived_class> : <base_class> {. . . }
class Shape { public void set. Width(int w) { width = w; } public void set. Height(int h) { height = h; } protected int width; protected int height; } // Derived class Rectangle: Shape { public int get. Area() { return (width * height); } } class Rectangle. Tester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect. set. Width(5); Rect. set. Height(7); // Print the area of the object. Console. Write. Line("Total area: {0}", Rect. get. Area()); Console. Read. Key(); } } O/P: Total area: 35
Visibility control � Class visibility and class members visibility ◦ public ◦ private ◦ protected ◦ internal ◦ protected internal � Accessibility of base class members ◦ All members of base class except constructors and destructors are inherited by derived class
class A { private int x; protected int y; public int z; } class B: A { public void Set. XYZ() { x=10; //Error: x is not accessible y=20; z=30; } }
Defining subclass constructors class Room { public int length; public int breadth; public Room(int x, int y) { length=x; breadth=y; } public int Area() { return (length * breadth); } }
class Hall: Room { int height; public Hall(int x, int y, int z): base(x, y) { height=z; } public int Volume() { return(length * breadth * height); } } class Inher. Test { public static void Main() { Hall room 1=new Hall(14, 12, 20); int area 1=room 1. Area(); int volume 1=room 1. Volume(); Console. Write. Line(“Area”+area 1); Console. Write. Line(“Volume”+volume 1); } } O/P: Area 168 Volume 3360
Overriding methods � When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding can be used. � Method in the derived class should have the same name, arguments and return type as the base class � Method overriding is possible only in derived classes, but not within the same class. � The base class method should be defined as virtual � The derived class method should be defined as override
Overriding methods � The overridden base method cannot be static or non-virtual � The overridden base method cannot be a sealed method � The accessibility of the method in the derived class cannot be changed
class Base. Class { public virtual string Your. City() { return "New York"; } } class Derived. Class : Base. Class { public override string Your. City() { return "London"; } } class Program { static void Main(string[] args) { Derived. Class obj = new Derived. Class(); string city = obj. Your. City(); Console. Write. Line(city); } } O/P: London
Hiding methods � To derive a class created by some one else and redefine the methods contained in it, use new keyword class Base { public void Display() { Console. Write. Line(“Base method”); } } class Derived : Base { public new void Display() { Console. Write. Line(“Derived method”); } } class Hide. Test { O/P: public static void Main() Derived method { Derived d=new Derived(); d. Display(); } }
Abstract class and Abstract methods � An abstract class cannot be instantiated � Abstract methods cannot have implementation � Its implementation must be provided in nonabstract derived classes by overriding the method � It can be declared only in abstract classes
abstract class Base. Class { public abstract string Your. City(); } class Derived. Class : Base. Class { public override string Your. City() //It is mandatory to implement absract method { return "London"; } } class Program { static void Main(string[] args) { Derived. Class obj = new Derived. Class(); string city = obj. Your. City(); Console. Write. Line(city); } } O/P: London
Sealed classes and Sealed methods � To prevent a class being derived further, it can be declared as sealed � An attempt to inherit sealed classes will cause an error � A sealed class cannot be an abstract class � A method in the derived class cannot override a sealed method class A { public virtual void Fun() { … } } class B: A { public sealed override void Fun() { … } }
Discussion
Mindmap
Stimulating Questions A door to a temple, a door to a house, a door to a wardrobe, all is doors, but all look different. What concept can this be related to?
Formative assessment 1. A class implements two interfaces each containing three methods. The class contains no instance data. Which of the following correctly indicate the size of the object created from this class? A. B. C. D. 12 bytes 24 bytes 0 byte 8 bytes Which of the following statements is correct about an interface used in C#. NET? A. One class can implement only one interface. B. In a program if one class implements an interface then no other class in the same program can implement this interface. C. From two base interfaces a new interface cannot be inherited. 2.
Formative assessment 3. Which of the following statements is correct about Interfaces used in C#. NET? A. B. C. D. E. All interfaces are derived from an Object class. Interfaces can be inherited. All interfaces are derived from an Object interface. Interfaces can contain only method declaration. Interfaces can contain static data and methods. 4. Which of the following should be used to implement a 'has a' relationship between two entities? A. B. C. D. E. Polymorphism Templates Containership Encapsulation Inheritance
Formative assessment 5. Which of the following should be used to implement a 'Like a' or a 'Kind of' relationship between two entities? A. B. C. D. E. Polymorphism Containership Templates Encapsulation Inheritance
- Slides: 31