Think With Java Mapping Real World Object with Java
A Society ( Group of families)
A Society ( Group of families)Everything in the World is an Object (Society, Family, Human, Cat, Dog, Car, River)
A society is a group of families.Families are collections of Family member.
Each Family member is Human or Non Human.
Each family member has some responsibilities (Activity) and they have their own properties.
Family members are interconnected to each other.
A Society ( Group of families)Each member is a human or non human.Human beings are persons.Each Human has common behavior comes from Animal.
Each family member uses their guardians vehicle or their own.
Each and every Human being has their own Address.
Humans(Objects) Behavior
Object is an entity which has State: determined by the values of its properties.
Behavior: determined by how an object performs an activity.
Humans(Objects) Properties & Behavior/ActivityEvery human(Male / Female / Other) has common properties and also they have their Behavior.
Properties: Name, Head, Hands, Hair, Eyes(color , size), Nose, Address.
Behavior: Walking, Running, Playing, Gaining Knowledge, Talking.
Map Now the Real World Objects
Java Class or Java BeanA simple Object instance container.It has some properties and behavior.So we can map our real world objects through Java .
Properties: nationalId, name, Address, vehicle,
PassportInfo, Occupation
Behavior: Walking, Running, Shopping,
Talking, Cycling,Driving
Access Modifier in java for properties and behaviorPublic: A declaration that is accessible to all classes with keyword “public”
Private: A declaration that is accessible only to the class in which it is declared with “private”.
Access Modifier in java for properties and behavior
Java OOP feature(Abstraction)
Customer should know to buy something
BillingStaff should know products pricing and to generate bill.
Administrator should product sell and to buy new products to gain business goal and to manage everything in the system.
ABSTRACTION: concept of identifying the essential details to be known and ignoring the non-essential details from the perspective of the end user.
Retail Shop Scenario
A simple Shopping Scenarioof Abstraction
EncapsulationThe Student don’t have to know the calculation mechanism of Calculator.
He/she just know its use of button. ENCAPSULATION: concept of hiding the
internal details and providing a simple interface to the end user which ensures that the system can be used without having to know how it works.
A Calculator Scenarioof Encapsulation
InheritanceEach Human have some common behavior of Animal.
Every family members are human or non-human(Cat or dog).
Each Regular or Privileged Customer have common Properties.
Different Inheritance
Inheritance
Allows to define the generalized and specialized characteristics and behavior. Multiple Inheritance
Inheritance
Allows to define the generalized and specialized characteristics and behavior.
Multi level Inheritance
Polymorphism Let us revisit the payment activity in the retail
shop Paymlent of bill can be done in two modes Cash(Calculation includes VAT) totalAmount = purchaseAmount + VAT Credit Card(Calculation includes processing
charge and VAT) totalAmount = purchaseAmount + VAT+
processingCharge POLYMORPHISM: concept which allows on
objecct/operation to behave differently in different situations.
So ? Now it is payment(processingCharge) or payment()?
PolymorphismPolymorphism is the capability of a method to do different things based on the object that it is acting upon
Following concepts demonstrate different types of polymorphism in java.
1) Method Overloading
2) Method Overriding
Static Binding vs Dynamic Bindingdifference between static and dynamic binding in Java.
Static binding happens at compile-time while dynamic binding happens at runtime.
Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. Binding of overridden methods happen at runtime.
Java uses static binding for overloaded methods and dynamic binding for overridden methods.
Dynamic Polymorphism(dynamic binding) example
Abstract class & abstract Method
If you declare an abstract method (discussed below) in a class then you must declare the class abstract as well. you can’t have abstract method in a non-abstract class.
What is an interface
Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.
** java programming language does not support multiple inheritance (we can do this through Interface)
Interface and abstractInterface provides complete abstraction as none of its methods can have body. On the other hand, abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both.
Use of Interface
Use of Interface with “abstract” keyword
Relationship in Java classes
Is A (Inheritance)Has A (Aggregation) Tightly coupledUses (Association) Loosely coupled
Relationship in Java classes
What next. . . . . . ORM = Object Relational MappingORM Providers : JPA, Hibernate, Spring, OFBiz
MVC = Model View ControllerMVC provider : JSF, Hibernate, Spring, OFBiz etc.
A Strong Tool : Collection Framework
To work with data in the local machine or memory must use the collection framework.
Collections--> Set, List, ArrayList, Map, LinkedHashMap
Collection Framework overview.
Collections Example
Questions & Answers Fun with Java :)
Thanks to all…Md. Arifur RahmanJunior Software Engineer,Newgen Technology Limited, Bangladesh.