Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

OOP Concepets and UML Class Diagrams

1,657 views

Published on

Inheritance,Encapsulation,Abstraction,Polymorphism,Virtual Methods,Generalization,Association,Aggregation,Composition

Published in: Technology
  • Be the first to comment

OOP Concepets and UML Class Diagrams

  1. 1. OOP/UML Concepts OOP Inheritance Inheritance occur when an object or a class is based on another object or a class. The idea is to reuse code and therefore reducing maintenance time. Class based inheritance is used in languages like C++,Java and C# while Object based inheritance (prototype) is used in languages such as JavaScript. public class MyException : Exception { public MyException (string message, string extrainfo) : base(message) { //other stuff here } } In this example MyException is a subclass of Exception. Above example also shows how to call the base-class constructor in C#. Encapsulation Encapsulation is like hiding something inside a capsule and giving access to safe to use methods only. Preventing unwanted access and avoiding risks. If Encapsulation is not used or not used properly it may lead to spaghetti code. Encapsulation reduces the risks. public class Employee { private BigDecimal salary = new BigDecimal(50000.00); public BigDecimal getSalary() { return salary; } public static void main() { Employee e = new Employee(); BigDecimal sal = e.getSalary(); } } class Program { public class Account { private decimal accountBalance = 500.00m; public decimal CheckBalance() { return accountBalance; } }
  2. 2. static void Main() { Account myAccount = new Account(); decimal myBalance = myAccount.CheckBalance(); /* This Main method can check the balance via the public * "CheckBalance" method provided by the "Account" class * but it cannot manipulate the value of "accountBalance" */ } } Abstraction Abstraction is used when you start your design process. You do not need to store all the details related to a real world concept in an object. As an example when we are creating an employee management system and we are using a class named Employee to identify an employee and there we do not need to store the length of his/her fingers. namespace EmployeeManagementSystem { public class Employee { private string address; private string name; private string telephone;
  3. 3. public string Address { get { return address; } set { address = value; } } public string Name { get { return name; } set { name = value; } } public string Telephone { get { return telephone; } set { telephone = value; } } public Employee(string name, string address, string telephone) { this.name = name; this.address = address; this.telephone = telephone; } } } Polymorphism Overloading (Ad hoc polymorphism) Overloaded methods are a part of polymorphism. It is the definition of multiple methods in same name but accepts different set of parameters. In this example draw() is an overloaded method. Overloading means that the same method name can be defined with more than one signature — the list of formal parameters (and their types). public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }
  4. 4. Subtyping Subtyping allows you to use the concept of ‘code to an interface’ to generalize objects. AnnHalloway and VictorBorga are musicians so they implement Musician interface. It is the ability to treat a class of object as if it is the parent class (or interface). public interface Musician { public void play(Work work); } public interface Work { public String getScript(); } public class FugaAndToccata implements Work { public String getScript() { return Bach.getFugaAndToccataScript(); } } public class AnnHalloway implements Musician { public void play(Work work) { // plays in her own style, strict, disciplined String script = work.getScript() } } public class VictorBorga implements Musician { public void play(Work work) { // goofing while playing with superb style String script = work.getScript() } } public class Listener { public void main(String[] args) { Musician musician; if (args!=null && args.length > 0 && args[0].equals("Anna")) { musician = new AnnHalloway(); } else { musician = new TerryGilliam(); } musician.play(new FugaAndToccata()); } Virtual Methods Virtual method is a method that can be overridden by a subclass. In Java all methods are virtual by default but in C# it has to be specified by the keyword virtual and overridden using override keyword. //Java public class A{ public int Test(){ return 0;
  5. 5. } } public class B extends A{ public int Test(){ return 1; } } //usage A a = new B(); int i = a.Test(); //1 is returned //C# public class A { public virtual int Test() { return 0; } } public class B : A { public override int Test() { return 1; } } //usage A a = new B(); int I = a.Test(); //1 is returned
  6. 6. UML Generalization In the above example Cats and Dogs are subclasses of Mammals. They override walk() method. Mammals is the super-class or the base-class of Cats and Dogs. Mammals is a generalized form of Cats and Dogs. Cats and Dogs are specialized forms of Mamals. The generalization relationship is also known as the inheritance or "is a" relationship. Association Person is a subscriber for Magazine. Person(s) subscribes Magazine(s). Magazine is a subscribed magazine for Person. Association represents the static relationship shared among the objects of two classes. Aggregation A Professor has Students. This is the weak variant of “has a” relation. Aggregation can occur when a class is a collection or container of other classes, but where the contained classes do not have a strong life cycle dependency on the container—essentially, if the container is destroyed, its contents are not.
  7. 7. Composition This is the strong variant of “has a” relation. Composition usually has a strong life cycle dependency between instances of the container class and instances of the contained class(es): If the container is destroyed, normally every instance that it contains is destroyed as well. (Note that, where allowed, a part can be removed from a composite before the composite is deleted, and thus not be deleted as part of the composite.) References  http://stackoverflow.com/questions/1031273/what-is-polymorphism  http://stackoverflow.com/questions/56867/interface-vs-base-class  http://en.wikipedia.org/wiki/Method_overriding  http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29  http://www.hiteshagrawal.com/uml/inheritance-in-uml  http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29  http://stackoverflow.com/questions/13123180/what-is-overloading-in-java  http://lcp.org.uk/wp-content/uploads/2012/08/rewards.jpg  http://en.wikipedia.org/wiki/Class_diagram  http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp

×