Oo concepts pdf


















You can change your ad preferences anytime. Next SlideShares. You are reading a preview. Create your free account to continue reading.

Sign Up. Upcoming SlideShare. Embed Size px. Start on. Show related SlideShares at end. WordPress Shortcode. Share Email. Top clipped slide. Download Now Download Download to read offline. Randy Connolly Follow. Object oriented methodologies. E learning presentation update OO Development 5 - Analysis. Dedicado a mis amig s ciberentic s. Sulucionario electromagnetismo cheng.

Beautiful Women Of China. Related Books Free with a 30 day trial from Scribd. Uncommon Carriers John McPhee. The Art of War Sun Tsu. Related Audiobooks Free with a 30 day trial from Scribd. Elizabeth Howell. They know how to do an amazing essay, research papers or dissertations. Maha Aldhaheri. Pravin Patil ,. Net technology, RPA developer at Cognizant. Yogendra Uikey. Ryan Spain. Show More. Views Total views. Actions Shares. No notes for slide.

OO Development 4 - Object Concepts 1. What is an Object? A ttribute Permitted A ccess N o acce ss attrib ute Fro m any class in the sam e p ackage pi uc b l Fro m any class anyw he re pa rt ie v N o acce ss fro m o utsid e class ped rc ot te Fro m any class in the sam e p ackage and fro m any sub class anyw he re Variable Scope 18 class 1 class 2 private variable A private variable X private variable B public variable Y public variable C public variable Z Instance variables in a class definition No object is created and no memory allocated for object.

Person employee; 2. Creates employee object in memory and sets employee to reference it. Inheritance 45 Person can be a is a Student Perhaps you can inherit from this existing class and code just the differences. The following class declarations produce the same exact results:. Object is implicitly included as a base class if it is not already declared. Besides providing the abstract glue to hold together the C class framework, object includes built-in functionality, some of which is useful for derived classes to implement.

A derived class may override a virtual method of the base class with the keyword override. The following restrictions must be followed. Sometimes derived class members have the same name as a corresponding base class member.

In this case, the derived member is said to be "hiding" the base class member. When hiding occurs, the derived member is masking the functionality of the base class member. Users of the derived class won't be able to see the hidden member; they'll see only the derived class member. The following code shows how hiding a base class member works. This means that when an instance of a SiteOwner class is invoked with a call to the FullAddress method, it is the SiteOwner class FullAddress method that is called, not the FullAddress method of the Contact class.

Although a base class member may be hidden, the derived class can still access it. It does this through the base identifier. Sometimes this is desirable. It is often useful to take advantage of the base class functionality and then add to it with the derived class code. The next example shows how to refer to a base class method from the derived class. In this particular example, the FullAddress method of the Contact class is called from within the FullAddress method of the SiteOwner class.

This is accomplished with a base class reference. This provides another way to reuse code and add on to it with customized behavior. Versioning, in the context of inheritance, is a C mechanism that allows modification of classes creating new versions without accidentally changing the meaning of the code. Hiding a base class member with the methods previously described generates a warning message from the compiler. This is because of the C versioning policy. It's designed to eliminate a class of problems associated with modifications to base classes.

Here's the scenario: A developer creates a class that inherits from a third-party library. For the purposes of this discussion, we assume that the Contact class represents the third-party library. Here's the example:. In this example, the FullAddress method does not exist in the base class.

There is no problem yet. Later on, the creators of the third-party library update their code. Part of this update includes a new member in a base class with the exact same name as the derived class:. In this code, the base class method FullAddress contains different functionality than the derived class method. In other languages, this scenario would break the code because of implicit polymorphism.

However, this does not break any code in C because when the FullAddress method is called on SiteOwner, it is still the SiteOwner class method that gets called. This scenario generates a warning message.

One way to eliminate the warning message is to place a new modifier in front of the derived class method name, as the following example shows:. This has the effect of explicitly letting the compiler know the developer's intent. Placing the new modifier in front of the derived class member states that the developers know there is a base class method with the same name, and they definitely want to hide that member.

This prevents breakage of existing code that depends on the implementation of the derived class member. With C , the method in the derived class is called when an object of the derived class type is used.

Likewise, the method in the base class is called when an object of the Base class type is called. Another problem this presents is that the base class may present some desirable new features that wouldn't be available through the derived class.

To use these new features requires one of a few different workarounds. One option would be to rename the derived class member, which would allow programs to use a base class method through a derived class member. The drawback to this option would be if there were other classes relying upon the implementation of the derived class member with the same name.

This scenario will break code and, for this reason, is considered extremely bad form. Another option is to define a new method in the derived class that called the base class method. This allows users of the derived class to have the new functionality of the base class, yet retain their existing functionality with the derived class.

While this would work, there are maintainability concerns for the derived class. Sealed classes are classes that can't be derived from. To prevent other classes from inheriting from a class, make it a sealed class.

There are a couple good reasons to create sealed classes, including optimization and security. Sealing a class avoids the system overhead associated with virtual methods. This allows the compiler to perform certain optimizations that are otherwise unavailable with normal classes. Another good reason to seal a class is for security. Inheritance, by its very nature, dictates a certain amount of protected access to the internals of a potential base class.

Sealing a class does away with the possibility of corruption by derived classes. A good example of a sealed class is the String class. The following example shows how to create a sealed class:. This example generates a compiler error. Since the CustomerStats class is sealed, it can't be inherited by the CustomerInfo class. The CustomerStats class was meant to be used as an encapsulated object in another class.

This is shown by the declaration of a CustomerStats object in the Customer class. Polymorphism is reflected in the ability to write one routine that can operate on objects from more than one class-treating different objects from different classes in exactly the same way. For instance, if both Customer and Vendor objects have a Name property, and we can write a routine that calls the Name property regardless of whether we're using a Customer or Vendor object, then we have polymorphism.

A vehicle is a good example of polymorphism. A vehicle interface would only have those properties and methods that all vehicles have, a few of which might include paint color, number of doors, accelerator, and ignition. These properties and methods would apply to all types of vehicles including cars, trucks, and semi-trucks.

Polymorphism will not implement code behind the vehicle's properties and methods. Instead, polymorphism is the implementation of an interface. If the car, truck, and semitruck all implement the same vehicle interface, then the client code for all three classes can be exactly the same. C gives us polymorphism through inheritance. C provides a keyword virtual that is used in the definition of a method to support polymorphism.

Child class are now free to provide their own implementation of this virtual method, that is called overriding. The following points are important regarding virtual keyword If the method is not virtual, the compiler simply uses the reference type to invoke the appropriate method.

If the method is virtual, the compiler will generate code to checkup the reference type at runtime it is actually denoting to, then the appropriate method is called from the class of the reference type. When a virtual method is called, runtime check late method binding is made to identify the object and appropriate method is invoked, all this is done at runtime.

In case of non-virtual methods, this information is available at compile time, so no runtime check to identify the object is made, so slightly efficient in the way non-virtual methods are called. But the behavior of virtual method is useful in many ways; the functionality they provide is fair enough to bear this slight loss of performance. The key factor here is the ability to dynamically invoke methods in a class based on their type.

Essentially, a program would have a group of objects, examine the type of each one, and execute the appropriate method. When we inherit above class, we have two choices to invoke constructor of the class. So this is an example of design time polymorphism. Here at design time we have to decide which method we need to invoke while inheriting the class. Polymorphism is the capability of a program to carry out dynamic operations by implementing methods of multiple derived classes through a common base class reference.

Another definition of polymorphism is the ability to treat different objects the same way. This means that the runtime type of an object determines its behavior rather than the compile-time type of its reference.

It was a long trip, but I tried to explain all the basics of Object Orientation in C with some practical examples. View All. Object Oriented Concepts in C. Tusharkant Agarwal Updated date Nov 15, Examples: You can create an abstraction of a dog with characteristics, such as color, height, and weight, and actions such as run and bite. The characteristics are called properties, and the actions are called methods. A Recordset object is an abstract representation of a set of data.

Classes are blueprints for Object. Objects are instance of classes. Access Modifiers Access Modifiers are keywords used to specify the declared accessibility of a member of a type. There are three main parts of Object: Interface Implementation or Behavior Member or Instance variables Interface The interface is defined as a set of methods Sub and Function routines , properties Property routines , events, and fields variables or attributes that are declared Public in scope.

Implementation or Behavior The code inside of a method is called the implementation. Return type of method. Member or Instance Variables The third key part of an object is its data, or state. Interface looks like a class, but has no implementation. Defining an Interface: MyInterface.

Using an Interface: InterfaceImplementer. WriteLine "MethodToImplement called. In this case, the following syntax is used: class InterfaceImplementer : IMyInterface Note that this class inherits the IMyInterface interface; it must implement its all members. Interface Inheritance: InterfaceInheritance. WriteLine "ParentInterfaceMethod called. Inheritance Inheritance is the idea that one class, called a subclass, can be based on another class, called a base class.

Inheritance is the ability to apply another class's interface and code to your own class. The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method.

GetInfo ; Console. Calling Base Class Members Derived classes can access the members of their base class if those members have protected or greater access.

More Tips regarding Inheritance: A static member cannot be marked as override, virtual, or abstract. So following is an error: public static virtual void GetSSN You can't call static methods of base class from derived class using base keyword. GetInfo from derived class instead you have to call Person. GetInfo from derived class. Inside Static members we can access only static fields, methods etc. Following example will give error, because we can't access name in GetInfo because name is not static.

If you are not overriding a virtual method of base class in derived class, you can't use base class method by using base keyword in derived class. Also when you will create an instance of derived class, it will call derived class method and you will only be able to access base class method when you will create instance of base class.

You can't decrease access level of a method in derived class when you are overriding a base class method in derived class, vice versa is possible.

Means you can make protected method of base class to public in derived class. The "this" keyword refers to: the current instance for which a method is called. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

Difference between Interface and Abstract Class Interfaces are closely related to abstract classes that have all members abstract. For an abstract class, at least one method of the class must be an abstract method that means it may have concrete methods. For an interface, all the methods must be abstract Class that implements an interface much provide concrete implementation of all the methods definition in an interface or else must be declare an abstract class In C , multiple inheritance is possible only through implementation of multiple interfaces.

Abstract class can only be derived once. An interface defines a contract and can only contains four entities viz methods, properties, events and indexes. An interface thus cannot contain constants, fields, operators, constructors, destructors, static constructors, or types. Also an interface cannot contain static members of any kind. The modifiers abstract, public, protected, internal, private, virtual, override is disallowed, as they make no sense in this context.

Class members that implement the interface members must be publicly accessible. Overriding Summary A derived class may override a virtual method of the base class with the keyword override. Keyword override is used in the definition of child class method that is going to override the base class's virtual method. The return type must be the same as the virtual method have in base class.

The name of the method should also be same. The parameter-list must also be same in order, number and type of parameters. The accessibility of the overriding method should not be more restricted than that of the accessibility defined with virtual method of the base class. This accessibility either be the same or less restricted.

The virtual methods can be sealed in the child or derived classes to prevent further modifications in the implementation of the virtual method in the derived classes, by declaring them as sealed methods. Hiding Base Class Members Sometimes derived class members have the same name as a corresponding base class member. Versioning in C Versioning, in the context of inheritance, is a C mechanism that allows modification of classes creating new versions without accidentally changing the meaning of the code.

SiteName, aSite. URL, aSite. Sealed Classes Sealed classes are classes that can't be derived from. The following points are important regarding virtual keyword:- If the method is not virtual, the compiler simply uses the reference type to invoke the appropriate method.

Implementing Polymorphism The key factor here is the ability to dynamically invoke methods in a class based on their type. Summary It was a long trip, but I tried to explain all the basics of Object Orientation in C with some practical examples. Abstraction C Encapsulation Inheritance Polymorphism. Next Recommended Reading. Net Core 6. Create A. Understanding Thread Starvation in.



0コメント

  • 1000 / 1000