SlideShare a Scribd company logo
1 of 7
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;
}
}
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;
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) {
...
}
}
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;
}
}
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
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.
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

More Related Content

What's hot

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationships
Pooja mittal
 

What's hot (20)

Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental Concepts
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
Class diagram
Class diagramClass diagram
Class diagram
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Class diagram- UML diagram
Class diagram- UML diagramClass diagram- UML diagram
Class diagram- UML diagram
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Abstract method
Abstract methodAbstract method
Abstract method
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationships
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
2 class use case
2 class use case2 class use case
2 class use case
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
class diagram
class diagramclass diagram
class diagram
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Uml class-diagram
Uml class-diagramUml class-diagram
Uml class-diagram
 
Class diagram
Class diagramClass diagram
Class diagram
 

Viewers also liked

UML Class Diagram G-3-122139
UML Class Diagram G-3-122139UML Class Diagram G-3-122139
UML Class Diagram G-3-122139
Hansi Thenuwara
 

Viewers also liked (7)

Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
 
OOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependencyOOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependency
 
UML Class Diagram G-3-122139
UML Class Diagram G-3-122139UML Class Diagram G-3-122139
UML Class Diagram G-3-122139
 
Object diagram
Object diagramObject diagram
Object diagram
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagrams
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 

Similar to OOP Concepets and UML Class Diagrams

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 

Similar to OOP Concepets and UML Class Diagrams (20)

Chap11
Chap11Chap11
Chap11
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Design pattern
Design patternDesign pattern
Design pattern
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Java02
Java02Java02
Java02
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Core java oop
Core java oopCore java oop
Core java oop
 
Java 06
Java 06Java 06
Java 06
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

OOP Concepets and UML Class Diagrams

  • 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. 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. 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. 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. } } 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. 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. 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