SlideShare a Scribd company logo
1 of 23
OOPs in Java
Ranjith Sekar
Jun 2015
Agenda
 Class
 SOLID Principles
 Object
 OOPs Concepts
 Access Modifiers
 Variables
Class
 Blueprint/template/contract
 Properties/Characteristics/Attributes & Actions
 Creation of a class
 Only one public class per source file
 Can contain multiple non-public classes (inner-classes)
 Example: Mobile
 Attributes (model, manufacturer, cost, operating system etc.,)
 Actions (call, file transfer, etc.,)
SOLID Principles
How to identify and design a Class?
 SRP (Single Responsibility Principle)
 OCP (Open Closed Principle)
 LSP (Liskov Substitution Principle)
 ISP (Interface Segregation Principle)
 DIP (Dependency Inversion Principle)
Single Responsibility Principle (SRP)
 A class should have only one reason to change (responsibility)
 Separation of responsibilities
 How to Recognize a Break of the SRP?
 Class Has Too Many Dependencies
 Method Has Too Many Parameters
 The Test Class Becomes Too Complicated
 Class / Method is Long (200-250 LOC)
 Descriptive Naming
 Change In One Place Breaks Another
 How to make the design compliant with the Single Responsibility Principle
 TDD
 Code Coverage Metrics
 Refactoring and Design Patterns
 Clear Modularization of the System
Open Closed Principle (OCP)
 Minimal changes on existing code (tested already)
 Open for extension but closed for modifications
 Inheritance using Interface/class
 Decorator Design Pattern
Liskov Substitution Principle (LSP)
 Derived types must be completely substitutable for their base types.
 Subtypes must be replaceable for the super type references without affecting the program
execution.
 Program to Interface, not the Implementation
Interface Segregation Principle (ISP)
 Clients should not be forced to implement interfaces they don't use
 Create different interfaces based on the method groups
 it produce a flexible design
Dependency Inversion Principle (DIP)
 High-level modules should not depend on low-level modules. Both should depend on
abstractions.
 Abstractions should not depend on details. Details should depend on abstractions.
 High Level Classes --> Abstraction Layer --> Low Level Classes
Object
 Any Real world entity
 Instance of class
 Stores States in Variables & Behaviors in methods
 Constructors – initialize during object creation
 Static blocks
How to Create an Object?
 Using ‘new’ keyword
List<String> obj1 = new ArrayList<String>();
 Using Class.forName() method
MyClass obj2 = Class.forName(“com.mycompany.MyClass”);
 Cloning
Person p1 = new Person();
Person p2 =(Person) p1.clone();
 Deserialization
ObjectInputStream inStream = new ObjectInputStream(null);
MyClass object = (MyClass) inStream.readObject();
 Class Loader
MyClass classLoader = (MyClass ) this.getClass().getClassLoader().loadClass("com.mycompany.MyClass ")
.newInstance();
OOPs Concepts
 Abstraction
 Inheritance
 Encapsulation
 Polymorphism
 Association
 Aggregation
 Composition
Abstraction
 Def: An abstraction denotes the essential characteristics of an object that distinguish it from
all other kinds of object and thus provide crisply defined conceptual boundaries, relative to
the perspective of the viewer
 What it does instead how it does (television)
 Decompose complex systems into smaller components
 Interface & Abstract Classes
 When Should We Use Abstraction
 when we want to achieve abstraction of data or actions
 when functionality is exposed to another component
Inheritance
 extends keyword
 Inherits/Shares the attributes and methods (public or protected) of existing classes
 Code reusability
 One Super-Class -> Multiple Sub-Class => Supported
 One Sub-Class-> Multiple Super-Class (Multiple Inheritance) – NOT Supported
 Compile time
 Super-Class & Sub-Class Constructor
 ‘super’ keyword - access the overridden method in the super class
 IS-A Relationship
 Sub-Class has Same attribute/method as in Super-Class – No inherit occur
 Types
 Single Inheritance
 Multiple Inheritance
 Multi-Level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
Encapsulation
 Hide the data & methods into Object
 Binding variables (instance variable) & methods into class
 Security
 Java Bean
 Also referred as ‘Block Box’
 private, public keyword
 Exposes only required things
 java.util.Hashtable
Polymorphism
 one name, many forms
 Overloading(compile-time/Static-binding)
 Method & operator overloading
 Overridding (run-time/Dynamic-binding)
 @Override annotation
 argument list & return type(covariant return type) must be same
 Final & private methods – Can’t Overridden
 Protected methods – can, but within package
 not applicable for static methods or variables (static and non-static)
 can throw any new unchecked (runtime) exception
 Public cannot be made default, but a default can be made public
 Shape Example (draw())
Association
 Relationship between two objects (one-to-one, one-to-many, many-to-one, many-to-many)
 Example: Teacher & Student
 own lifecycle and there is no owner
 Both can exists without each other
Aggregation
 “Has-a” relationship
 Special form of Association
 One object is owner for other
 Example: Teacher & Department
 own lifecycle but child object can not belongs to another parent object
Composition
 Special form of Aggregation
 Child object dose not have their lifecycle
 All the child objects will be deleted if Parent object deleted
 Example: House & Room
OO design Principles & OO basic principles
Access Modifiers
 default
 Public
 Private
 protected
Variables
 Instance Variables (Non-Static Fields)
 Class Variables (Static Fields)
 Local Variables (method level variables)
Thank You All 

More Related Content

What's hot

What's hot (20)

Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
java token
java tokenjava token
java token
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Oop java
Oop javaOop java
Oop java
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Interface
InterfaceInterface
Interface
 

Viewers also liked

Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentalsjavaease
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of javavinay arora
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for DesignersR. Sosa
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
Core java Basics
Core java BasicsCore java Basics
Core java BasicsRAMU KOLLI
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basicsmhtspvtltd
 

Viewers also liked (20)

Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
Java Basics
Java BasicsJava Basics
Java Basics
 
PALASH SL GUPTA
PALASH SL GUPTAPALASH SL GUPTA
PALASH SL GUPTA
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Java basics
Java basicsJava basics
Java basics
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
Java basics
Java basicsJava basics
Java basics
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 

Similar to OOPs in Java

Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptxSajidTk2
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Java Interview Questions For Freshers
Java Interview Questions For FreshersJava Interview Questions For Freshers
Java Interview Questions For Fresherszynofustechnology
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindiappsdevelopment
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...ShuvrojitMajumder
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2Usman Mehmood
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 

Similar to OOPs in Java (20)

OOPS in Java
OOPS in JavaOOPS in Java
OOPS in Java
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
 
Oops
OopsOops
Oops
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Java session2
Java session2Java session2
Java session2
 
Java Interview Questions For Freshers
Java Interview Questions For FreshersJava Interview Questions For Freshers
Java Interview Questions For Freshers
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
 
Unit 3
Unit 3Unit 3
Unit 3
 
Csci360 20 (1)
Csci360 20 (1)Csci360 20 (1)
Csci360 20 (1)
 
Csci360 20
Csci360 20Csci360 20
Csci360 20
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 

Recently uploaded

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Recently uploaded (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

OOPs in Java

  • 1. OOPs in Java Ranjith Sekar Jun 2015
  • 2. Agenda  Class  SOLID Principles  Object  OOPs Concepts  Access Modifiers  Variables
  • 3. Class  Blueprint/template/contract  Properties/Characteristics/Attributes & Actions  Creation of a class  Only one public class per source file  Can contain multiple non-public classes (inner-classes)  Example: Mobile  Attributes (model, manufacturer, cost, operating system etc.,)  Actions (call, file transfer, etc.,)
  • 4. SOLID Principles How to identify and design a Class?  SRP (Single Responsibility Principle)  OCP (Open Closed Principle)  LSP (Liskov Substitution Principle)  ISP (Interface Segregation Principle)  DIP (Dependency Inversion Principle)
  • 5. Single Responsibility Principle (SRP)  A class should have only one reason to change (responsibility)  Separation of responsibilities  How to Recognize a Break of the SRP?  Class Has Too Many Dependencies  Method Has Too Many Parameters  The Test Class Becomes Too Complicated  Class / Method is Long (200-250 LOC)  Descriptive Naming  Change In One Place Breaks Another  How to make the design compliant with the Single Responsibility Principle  TDD  Code Coverage Metrics  Refactoring and Design Patterns  Clear Modularization of the System
  • 6. Open Closed Principle (OCP)  Minimal changes on existing code (tested already)  Open for extension but closed for modifications  Inheritance using Interface/class  Decorator Design Pattern
  • 7. Liskov Substitution Principle (LSP)  Derived types must be completely substitutable for their base types.  Subtypes must be replaceable for the super type references without affecting the program execution.  Program to Interface, not the Implementation
  • 8. Interface Segregation Principle (ISP)  Clients should not be forced to implement interfaces they don't use  Create different interfaces based on the method groups  it produce a flexible design
  • 9. Dependency Inversion Principle (DIP)  High-level modules should not depend on low-level modules. Both should depend on abstractions.  Abstractions should not depend on details. Details should depend on abstractions.  High Level Classes --> Abstraction Layer --> Low Level Classes
  • 10. Object  Any Real world entity  Instance of class  Stores States in Variables & Behaviors in methods  Constructors – initialize during object creation  Static blocks
  • 11. How to Create an Object?  Using ‘new’ keyword List<String> obj1 = new ArrayList<String>();  Using Class.forName() method MyClass obj2 = Class.forName(“com.mycompany.MyClass”);  Cloning Person p1 = new Person(); Person p2 =(Person) p1.clone();  Deserialization ObjectInputStream inStream = new ObjectInputStream(null); MyClass object = (MyClass) inStream.readObject();  Class Loader MyClass classLoader = (MyClass ) this.getClass().getClassLoader().loadClass("com.mycompany.MyClass ") .newInstance();
  • 12. OOPs Concepts  Abstraction  Inheritance  Encapsulation  Polymorphism  Association  Aggregation  Composition
  • 13. Abstraction  Def: An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer  What it does instead how it does (television)  Decompose complex systems into smaller components  Interface & Abstract Classes  When Should We Use Abstraction  when we want to achieve abstraction of data or actions  when functionality is exposed to another component
  • 14. Inheritance  extends keyword  Inherits/Shares the attributes and methods (public or protected) of existing classes  Code reusability  One Super-Class -> Multiple Sub-Class => Supported  One Sub-Class-> Multiple Super-Class (Multiple Inheritance) – NOT Supported  Compile time  Super-Class & Sub-Class Constructor  ‘super’ keyword - access the overridden method in the super class  IS-A Relationship  Sub-Class has Same attribute/method as in Super-Class – No inherit occur  Types  Single Inheritance  Multiple Inheritance  Multi-Level Inheritance  Hierarchical Inheritance  Hybrid Inheritance
  • 15. Encapsulation  Hide the data & methods into Object  Binding variables (instance variable) & methods into class  Security  Java Bean  Also referred as ‘Block Box’  private, public keyword  Exposes only required things  java.util.Hashtable
  • 16. Polymorphism  one name, many forms  Overloading(compile-time/Static-binding)  Method & operator overloading  Overridding (run-time/Dynamic-binding)  @Override annotation  argument list & return type(covariant return type) must be same  Final & private methods – Can’t Overridden  Protected methods – can, but within package  not applicable for static methods or variables (static and non-static)  can throw any new unchecked (runtime) exception  Public cannot be made default, but a default can be made public  Shape Example (draw())
  • 17. Association  Relationship between two objects (one-to-one, one-to-many, many-to-one, many-to-many)  Example: Teacher & Student  own lifecycle and there is no owner  Both can exists without each other
  • 18. Aggregation  “Has-a” relationship  Special form of Association  One object is owner for other  Example: Teacher & Department  own lifecycle but child object can not belongs to another parent object
  • 19. Composition  Special form of Aggregation  Child object dose not have their lifecycle  All the child objects will be deleted if Parent object deleted  Example: House & Room
  • 20. OO design Principles & OO basic principles
  • 21. Access Modifiers  default  Public  Private  protected
  • 22. Variables  Instance Variables (Non-Static Fields)  Class Variables (Static Fields)  Local Variables (method level variables)