SlideShare a Scribd company logo
Java Class Design Ganesh Samarthyam
ganesh@codeops.tech
Name the first
object oriented
programming
language
Simula (1965!)
Java Class Design
Java Class Design
OOP sometimes becomes
❖ Uh-oh
or
❖ Oops!
Guess the output
class Base {
	 public Base() {
	 	 foo();
	 }
	 public void foo() {
	 	 System.out.println("In Base's foo ");
	 }
}
class Derived extends Base {
	 public Derived() {
	 	 i = new Integer(10);
	 }
	 public void foo() {
	 	 System.out.println("In Derived's foo " + i.toString() );
	 }
	 private Integer i;
}
class Test {
	 public static void main(String [] s) {
	 	 new Derived();
	 }
}
Guess the output
abstract class Printer {
private Integer portNumber = getPortNumber();
abstract Integer getPortNumber();
public static void main(String[]s) {
Printer p = new LPDPrinter();
System.out.println(p.portNumber);
}
}
class LPDPrinter extends Printer {
/* Line Printer Deamon port no is 515 */
private Integer defaultPortNumber = 515;
Integer getPortNumber() {
return defaultPortNumber;
}
}
Date & time exercise
❖ Your project requires support for date and time
functionality
❖ Assume that the Java library or third-party library support is
not available for date and time related functionality
❖ Will you provide separate classes for date, time and
timezone or a single class that supports all the three? How to
support calculating differences between two date/time
values?
Class design problem: Example
Discussion example
// using java.util.Date
Date today = new Date();
System.out.println(today);
$ java DateUse
Wed Dec 02 17:17:08 IST 2015
Why should we get the
time and timezone details
if I only want a date? Can
I get rid of these parts?
No!
So what!
Date today = new Date();
System.out.println(today);
Date todayAgain = new Date();
System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
Thu Mar 17 13:21:55 IST 2016
Thu Mar 17 13:21:55 IST 2016
false
What is going
on here?
java.time package!
Solution
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate todayAgain = LocalDate.now();
System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
2016-03-17
2016-03-17
true
Works fine
now!
Revisiting the example …
You can use only date,
time, or even timezone,
and combine them as
needed!
LocalDate today = LocalDate.now();
System.out.println(today);
LocalTime now = LocalTime.now();
System.out.println(now);
ZoneId id = ZoneId.of("Asia/Tokyo");
System.out.println(id);
LocalDateTime todayAndNow = LocalDateTime.now();
System.out.println(todayAndNow);
ZonedDateTime todayAndNowInTokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(todayAndNowInTokyo);
2016-03-17
13:28:06.927
Asia/Tokyo
2016-03-17T13:28:06.928
2016-03-17T16:58:06.929+09:00[Asia/Tokyo]
More classes in Date/Time API
Avoid switch cases
public'Insets'getBorderInsets(Component'c,'Insets'insets){'
''''''''Insets'margin'='null;'
'''''''''//'Ideally'we'd'have'an'interface'defined'for'classes'which'
''''''''//'support'margins'(to'avoid'this'hackery),'but'we've'
''''''''//'decided'against'it'for'simplicity'
''''''''//'
''''''''if'(c'instanceof'AbstractBuEon)'{'
'''''''''''''''''margin'='((AbstractBuEon)c).getMargin();'
''''''''}'else'if'(c'instanceof'JToolBar)'{'
''''''''''''''''margin'='((JToolBar)c).getMargin();'
''''''''}'else'if'(c'instanceof'JTextComponent)'{'
''''''''''''''''margin'='((JTextComponent)c).getMargin();'
''''''''}'
''''''''//'rest'of'the'code'omiEed'…'
Interface hierarchy for polymorphism
Using runtime polymorphism
!!!!!!!margin!=!c.getMargin();
!!!!!!!!if!(c!instanceof!AbstractBu8on)!{!
!!!!!!!!!!!!!!!!!margin!=!((AbstractBu8on)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JToolBar)!{!
!!!!!!!!!!!!!!!!margin!=!((JToolBar)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JTextComponent)!{!
!!!!!!!!!!!!!!!!margin!=!((JTextComponent)c).getMargin();!
!!!!!!!!}
Assign responsibilities correctly
class	GraphicsDevice
public	void	setFullScreenWindow(Window	 w)	{
if	(w	!=	null)	{
if	(w.getShape() !=	null)	{	w.setShape(null); }
if	(w.getOpacity() <	1.0f)	{	w.setOpacity(1.0f);	}
if	(!w.isOpaque())	{
Color	bgColor =	w.getBackground();
bgColor =	new	Color(bgColor.getRed(),	
bgColor.getGreen(),
bgColor.getBlue(),	255);
w.setBackground(bgColor);
}
}
…
}
Keep data members private
Protect from direct external mutations
Ensure each class has a single responsibility
Adhere to IS-A relationship
Depend on abstractions
SOLID Principles
S
Single Responsibility
Principle
Every object should have a single responsibility and
that should be encapsulated by the class
O Open Closed Principle
Software should be open for extension, but closed for
modification
L
Liskov’s Substitution
Principle
Any subclass should always be usable instead of its
parent class
I
Interface Segregation
Principle
Many client specific interfaces are better than one
general purpose interface
D
Dependency Inversion
Principle
Abstractions should not depend upon details. Details
should depend upon abstractions
Tools to explore
Jhawk
(Java)
CodeCity
(C++,	Java,	C#)
CppDepend
(C++)
Sotograph
(C++,	Java,	C#)
Imagix 4D	
(C,	C++,	Java)
Lattix
(C/C++,	Java,	C#)
SolidSX
(C++,	Java,	C#)
Bauhaus
(C/C++,	Java,	C#)
Structure101	
(Java,	C#)
Understand	
(C/C++,	Java,	C#)
Simian
(C/C++,	Java,	C#,	…)
Jarchitect
(Java)
Ndepend
(C#)
Stan4J
(Java)
InFusion
(C/C++,	Java)
InCode
(C/C++,	Java)
Meetups
h"p://www.meetup.com/JavaScript-Meetup-Bangalore/	
h"p://www.meetup.com/Container-Developers-Meetup-Bangalore/		
h"p://www.meetup.com/So>ware-Cra>smanship-Bangalore-Meetup/	
h"p://www.meetup.com/Core-Java-Meetup-Bangalore/	
h"p://www.meetup.com/CloudOps-Meetup-Bangalore/	
h"p://www.meetup.com/Bangalore-SDN-IoT-NetworkVirtualizaGon-Enthusiasts/	
h"p://www.meetup.com/So>wareArchitectsBangalore/	
h"ps://www.meetup.com/Mobile-App-Developers-Bangalore-Meetup/
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg

More Related Content

PDF
Java Generics - by Example
PDF
Design Patterns - Compiler Case Study - Hands-on Examples
PDF
Advanced Debugging Using Java Bytecodes
PDF
Software Design in Practice (with Java examples)
PDF
Java Class Design
PDF
Coding Guidelines - Crafting Clean Code
PDF
Python Performance 101
PPSX
Tuga it 2016 - What's New In C# 6
Java Generics - by Example
Design Patterns - Compiler Case Study - Hands-on Examples
Advanced Debugging Using Java Bytecodes
Software Design in Practice (with Java examples)
Java Class Design
Coding Guidelines - Crafting Clean Code
Python Performance 101
Tuga it 2016 - What's New In C# 6

What's hot (19)

PPT
Euro python2011 High Performance Python
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PPT
Profiling and optimization
PDF
Java 8 Lambda Expressions
PPT
Java Generics for Dummies
KEY
Why Learn Python?
PDF
The best language in the world
PDF
Lambdas and Streams Master Class Part 2
PPSX
What's new in C# 6 - NetPonto Porto 20160116
PDF
Java VS Python
PPTX
Java Generics
PDF
Java 8 Stream API. A different way to process collections.
PDF
Что нам готовит грядущий C#7?
PDF
FP in Java - Project Lambda and beyond
PPTX
07. Java Array, Set and Maps
PPTX
Lambda выражения и Java 8
PDF
From object oriented to functional domain modeling
PDF
Java 8 Workshop
PDF
Kotlin Bytecode Generation and Runtime Performance
Euro python2011 High Performance Python
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Profiling and optimization
Java 8 Lambda Expressions
Java Generics for Dummies
Why Learn Python?
The best language in the world
Lambdas and Streams Master Class Part 2
What's new in C# 6 - NetPonto Porto 20160116
Java VS Python
Java Generics
Java 8 Stream API. A different way to process collections.
Что нам готовит грядущий C#7?
FP in Java - Project Lambda and beyond
07. Java Array, Set and Maps
Lambda выражения и Java 8
From object oriented to functional domain modeling
Java 8 Workshop
Kotlin Bytecode Generation and Runtime Performance
Ad

Viewers also liked (6)

PDF
Core Java: Best practices and bytecodes quiz
PDF
Java Generics - by Example
PPTX
Java 8 concurrency abstractions
PDF
Java Concurrency by Example
PDF
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
PDF
SOLID Principles and Design Patterns
Core Java: Best practices and bytecodes quiz
Java Generics - by Example
Java 8 concurrency abstractions
Java Concurrency by Example
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
SOLID Principles and Design Patterns
Ad

Similar to Java Class Design (20)

PDF
Java 8 Date and Time API
PDF
Java 8 date & time api
PPTX
Java dates
PPTX
JSR 310. New Date API in Java 8
PPTX
Java 8 Date and Time API
PPTX
Java22_1670144363.pptx
PPTX
Date time java 8 (jsr 310)
PPTX
Java 8 Date-Time API
PPTX
A JSR-310 Date: Beyond JODA Time
PPTX
Computer programming 2 Lesson 14
PPTX
Java utility classes
PDF
Brand New Date and Time API
PDF
Brand new Date and Time API
PDF
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
PDF
Introduction to Date and Time API 3
PDF
Joda-Time & JSR 310 – Problems, Concepts and Approaches
PDF
Introduction to Date and Time API 3
PDF
Introduction to Java 8 java.time
PPTX
Java 8
Java 8 Date and Time API
Java 8 date & time api
Java dates
JSR 310. New Date API in Java 8
Java 8 Date and Time API
Java22_1670144363.pptx
Date time java 8 (jsr 310)
Java 8 Date-Time API
A JSR-310 Date: Beyond JODA Time
Computer programming 2 Lesson 14
Java utility classes
Brand New Date and Time API
Brand new Date and Time API
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
Introduction to Date and Time API 3
Joda-Time & JSR 310 – Problems, Concepts and Approaches
Introduction to Date and Time API 3
Introduction to Java 8 java.time
Java 8

More from Ganesh Samarthyam (20)

PDF
Wonders of the Sea
PDF
Animals - for kids
PDF
Applying Refactoring Tools in Practice
PDF
CFP - 1st Workshop on “AI Meets Blockchain”
PDF
Great Coding Skills Aren't Enough
PDF
College Project - Java Disassembler - Description
PDF
Bangalore Container Conference 2017 - Brief Presentation
PDF
Bangalore Container Conference 2017 - Poster
PDF
OO Design and Design Patterns in C++
PDF
Bangalore Container Conference 2017 - Sponsorship Deck
PDF
Let's Go: Introduction to Google's Go Programming Language
PPT
Google's Go Programming Language - Introduction
PDF
Java Generics - Quiz Questions
PDF
Software Architecture - Quiz Questions
PDF
Docker by Example - Quiz
PDF
Refactoring for Software Architecture Smells - International Workshop on Refa...
PDF
Refactoring for Software Architecture Smells - International Workshop on Refa...
PDF
Refactoring for Software Design Smells - XP Conference - August 20th 2016
PDF
Writing an Abstract - Template (for research papers)
PDF
How to Write Abstracts (for White Papers, Research Papers, ...)
Wonders of the Sea
Animals - for kids
Applying Refactoring Tools in Practice
CFP - 1st Workshop on “AI Meets Blockchain”
Great Coding Skills Aren't Enough
College Project - Java Disassembler - Description
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Poster
OO Design and Design Patterns in C++
Bangalore Container Conference 2017 - Sponsorship Deck
Let's Go: Introduction to Google's Go Programming Language
Google's Go Programming Language - Introduction
Java Generics - Quiz Questions
Software Architecture - Quiz Questions
Docker by Example - Quiz
Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Design Smells - XP Conference - August 20th 2016
Writing an Abstract - Template (for research papers)
How to Write Abstracts (for White Papers, Research Papers, ...)

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPT
Introduction Database Management System for Course Database
PDF
Digital Strategies for Manufacturing Companies
PDF
medical staffing services at VALiNTRY
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ai tools demonstartion for schools and inter college
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Online Work Permit System for Fast Permit Processing
Wondershare Filmora 15 Crack With Activation Key [2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
2025 Textile ERP Trends: SAP, Odoo & Oracle
Odoo Companies in India – Driving Business Transformation.pdf
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Introduction Database Management System for Course Database
Digital Strategies for Manufacturing Companies
medical staffing services at VALiNTRY
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ai tools demonstartion for schools and inter college
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
CHAPTER 2 - PM Management and IT Context
Adobe Illustrator 28.6 Crack My Vision of Vector Design
System and Network Administration Chapter 2
Design an Analysis of Algorithms II-SECS-1021-03
Online Work Permit System for Fast Permit Processing

Java Class Design