Introduction to Object Oriented Programming Concepts (OOP) and More - CodeProject
Introduction to Object-Oriented Programming Using C++. name,; size,; date of birth,; shape,; social number,; room number,; hair colour,; hobbies. .. The previous relationship is used at the class level to describe relationships between two. Hi all I am bit confusing with is-a, has-a and uses-a relationship of class, I know it's a basic concepts of OOPS and Inheritance. Is there any. Difference between Inheritance and Composition in Java OOPS Pour This On Your Head And Watch What HappensRevitalizing Hair Therapy| The UML diagram is incorrect for composition relationship and inheritance.
In order to manage such a work force, you need to have proper management policies in place.
Difference between Inheritance and Composition in Java OOPS
Same technique can be applied to manage classes of your software system. In order to manage the classes of a software system, and to reduce the complexity, system designers use several techniques, which can be grouped under four main concepts named 1. What is Encapsulation or Information Hiding? The encapsulation is the inclusion-within a program object-of all the resources needed for the object to function, basically, the methods and the data.
In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties.
A class is kind of a container or capsule or a cell, which encapsulate a set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system.
That idea of encapsulation is to hide how a class does its business, while allowing other classes to make requests of it. According to Object Oriented Programming there are several techniques classes can use to link with each other. There are several other ways that an encapsulation can be used, as an example we can take the usage of an interface.
The interface can be used to hide the information of an implemented class. It allows one object instance to cause another to perform an action on its behalf. Association is the more general term that define the relationship between two classes, where as the aggregation and composition are relatively special.
Since a direction is explicitly specified, in this case the controller class is the StudentRegistrar. To some beginners, association is a confusing concept.
The troubles created not only by the association alone, but with two other OOP concepts, that is association, aggregation and composition. Every one understands association, before aggregation and composition are described. Aggregation or composition cannot be separately understood. What is the difference between Association, Aggregation, and Composition?
It is just the connectivity between the two classes. Aggregation is a weak type of Association with partial ownership. This is weak compared to Composition. Then again, weak meaning the linked components of the aggregator may survive the aggregations life-cycle without the existence of their parent objects.
Any teacher may belong to more than one department. And so, if a department ceases to exist, the teacher will still exist. On the other hand, Composition is a strong type of Association with full ownership. Hence, if a department ceases to exist, the underlying courses will cease to exist as well. There is no ownership between the teacher and the student, and each has their own life-cycle. But even without a Chancellor a University can exists. But the Faculties cannot exist without the University, the life time of a Faculty or Faculties attached with the life time of the University.
If University is disposed the Faculties will not exist. In that case we called that University is composed of Faculties. Same way, as another example, you can say that, there is a composite relationship in-between a KeyValuePairCollection and a KeyValuePair.
I see Composition is being used in many other ways too. However the more important factor, that most people forget is the life time factor. The life time of the two classes that has bond with a composite relation mutually depend on each other.
If you take the. NET Collection to understand this, there you have the Collection element define inside it is an inner part, hence called it is composed of the Collection, farcing the Element to get disposed with the Collection. So the point is, if you want to bind two classes with Composite relation, more accurate way is to have a one define inside the other class making it a protected or private class. This way you are allowing the outer class to fulfill its purpose, while tying the lifetime of the inner class with the outer class.
So in summary, we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation. What is Abstraction and Generalization? Abstraction is an emphasis on the idea, qualities and properties rather than the particulars a suppression of detail.
- Relationship is-a, has-a and uses-a
- Table of contents
- Re: Relationship is-a, has-a and uses-a
The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects.
Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs. While abstraction reduces complexity by hiding irrelevant detail, generalization reduces complexity by replacing multiple entities which perform similar functions with a single construct.
Generalization is the broadening of application to encompass a larger domain of objects of the same or different type. Programming languages provide generalization through variables, parameterization, generics and polymorphism. It places the emphasis on the similarities between objects. Thus, it helps to manage complexity by collecting individuals into groups and providing a representative which can be used to specify any individual of the group.
Abstraction and generalization are often used together. Abstracts are generalized through parameterization to provide greater utility. In parameterization, one or more parts of an entity are replaced with a name which is new to the entity. The name is used as a parameter. When the parameterized abstract is invoked, it is invoked with a binding of the parameter to an argument.
What is an Abstract class? Abstract classes, which declared with the abstract keyword, cannot be instantiated. It can only be used as a super-class for other classes that extend the abstract class. Abstract class is the concept and implementation gets completed when it is being realized by a subclass. Abstract classes are ideal when implementing frameworks. Please carefully read the comments as it will help you to understand the reasoning behind this code.
This class will allow all subclass to gain access to a common exception logging module and will facilitate to easily replace the logging library. But you do have a concept in mind and that is, if a class is going to log an exception, they have to inherit the LoggerBase. In other word the LoggerBase provide a framework for exception logging. Like any other class, an abstract class can contain fields, hence I used a private field named logger declare the ILog interface of the famous log4net library.
This will allow the Loggerbase class to control, what to use, for logging, hence, will allow changing the source logger library easily. The access modifier of the constructor of the LoggerBase is protected.
The public constructor has no use when the class is of type abstract. The abstract classes are not allowed to instantiate the class. So I went for the protected constructor. The abstract property named LogPrefix is an important one. It enforces and guarantees to have a value for LogPrefix LogPrefix uses to obtain the detail of the source class, which the exception has occurred for every subclass, before they invoke a method to log an error.
The method named LogError is protected, hence exposed to all subclasses. You are not allowed or rather you cannot make it public, as any class, without inheriting the LoggerBase cannot use it meaningfully. Apart from these you can also have virtual methods defined in an abstract class. The virtual method may have its default implementation, where a subclass can override it when required. All and all, the important factor here is that all OOP concepts should be used carefully with reasons, you should be able to logically explain, why you make a property a public or a field a private or a class an abstract.
What is an Interface? In summary the Interface separates the implementation and defines the structure, and this concept is very useful in cases where you need the implementation to be interchangeable. Apart from that an interface is very useful when the implementation changes frequently. Some say you should define all classes in terms of interfaces, but I think recommendation seems a bit extreme. Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.
Interfaces just specify the method declaration implicitly public and abstract and can contain properties which are also implicitly public and abstract. Interface definition begins with the keyword interface. An interface like that of an abstract class cannot be instantiated. If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and the method definitions must be provided by the subclass that extends the abstract class.
In addition to this an interfaces can inherit other interfaces. The sample below will provide an interface for our LoggerBase abstract class. What is the difference between a Class and an Interface? When a class implements an interface, an object of such class can be encapsulated inside an interface. Theoretically a class emphasis the idea of encapsulation, while an interface emphasis the idea of abstraction by suppressing the details of the implementation.
The two poses a clear separation from one to another. Therefore it is very difficult or rather impossible to have an effective meaningful comparison between the two, but it is very useful and also meaningful to have a comparison between an interface and an abstract class. What is the difference between an Interface and an Abstract class?
There are quite a big difference between an interface and an abstract class, even though both look similar. Interface definition begins with a keyword interface so it is of type interface Abstract classes are declared with the abstract keyword so it is of type class Interface has no implementation, but they have to be implemented.
Interface can inherit more than one interfaces Abstract class can implement more than one interfaces, but can inherit only one class Abstract class must override all abstract method and may override virtual methods Interface can be used when the implementation is changing Abstract class can be used to provide some default behavior for a base class.
Interface makes implementation interchangeable Interface increase security by hiding the implementation Abstract class can be used when implementing framework Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies.
Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class can be used to provide the default implementation of the services and all mandatory modules such as event logging and message handling etc. This approach allows the developers to develop the application within the guided help provided by the framework.
However, in practice when you come across with some application-specific functionality that only your application can perform, such as startup and shutdown tasks etc. The abstract base class can declare virtual shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions.
When it is time to start up, the abstract class can call the startup method. When the base class calls this method, it can execute the method defined by the child class. What are Implicit and Explicit Interface Implementations? Net support multiple implementations, the concept of implicit and explicit implementation provide safe way to implement methods of multiple interfaces by hiding, exposing or preserving identities of each of interface methods, even when the method signatures are the same.
Let's consider the interfaces defined below. The ability of a new class to be created, from an existing class by extending it, is called inheritance. The class IOException can extend the functionality of the class Exception by adding new types and methods and by overriding existing ones. Just like abstraction is closely related with generalization, the inheritance is closely related with specialization.
Introduction to Object Oriented Programming Concepts (OOP) and More
It is important to discuss those two concepts together with generalization to better understand and to reduce the complexity. When we say that a dog is a mammal, we mean that the dog is a specialized kind of mammal.
It has all the characteristics of any mammal it bears live young, nurses with milk, has hairbut it specializes these characteristics to the familiar characteristics of canis domesticus.Object Oriented programming ( OOP ) :- What is Aggregation , Association and Composition ?
A cat is also a mammal. As such, we expect it to share certain characteristics with the dog that are generalized in Mammal, but to differ in those characteristics that are specialized in cats. The specialization and generalization relationships are both reciprocal and hierarchical.
Specialization is just the other side of the generalization coin: Mammal generalizes what is common between dogs and cats, and dogs and cats specialize mammals to their own specific subtypes. They have all characteristics and behaviors of an Exception, That mean the IOException is a specialized kind of Exception. A SecurityException is also an Exception.
Types Of Relationships In Object Oriented Programming (OOPS)
As such, we expect it to share certain characteristic with IOException that are generalized in Exception, but to differ in those characteristics that are specialized in SecurityExceptions. In OOP, the specialization relationship is implemented using the principle called inheritance. This is the most common and most natural and widely accepted way of implement this relationship. Polymorphisms is a generic term that means 'many shapes'. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.
At times, I used to think that understanding Object Oriented Programming concepts have made it difficult since they have grouped under four main concepts, while each concept is closely related with one another.
Hence one has to be extremely careful to correctly understand each concept separately, while understanding the way each related with other concepts. In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading, and method overriding, 4.
What is Method Overloading? Method overloading is the ability to define several methods all with the same name.
What is Operator Overloading? What is Method Overriding? Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes. A subclass can give its own definition of methods but need to have the same signature as the method in its super-class.
This means that when overriding a method the subclass's method has to have the same name and parameter list as the super-class' overridden method.
I will go point by point and try to explain each point in as much detail as possible without boring you: When you use Inheritance, you have to define which class you are extending in code, it cannot be changed at runtime, but with Composition you just define a Type which you want to use, which can hold its different implementation.
In this sense, Composition is much more flexible than Inheritance. If you want to leverage functionalities from multiple class, you must use Composition. For example, if your code needs authentication functionality, you can use an Authenticater, for authorization you can use an Authorizer etc, but with Inheritance you just stuck with only class, Why? This difference between Inheritance vs Composition actually highlight a severe limitation of later.
When you design classes using Composition they are easier to test because you can supply mock implementation of the classes you are using but when you design your class using Inheritance, you must need parent class in order to test child class.
Their is no way you can provide mock implementation of parent class. Though both Inheritance and Composition allows code reuse, Inheritance breaks encapsulation because in case of Inheritance, sub class is dependent upon super class behavior. If parent classes changes its behavior than child class is also get affected.