SlideShare a Scribd company logo
www.SunilOS.com 1
Object Oriented Programming
OOP……. Not OOPS!!
Core Concepts
www.sunilos.com
www.raystec.com
2
Java Primitive Data Types
Primitive Data Types:
o boolean true or false
o char unicode (16 bits)
o byte signed 8 bit integer
o short signed 16 bit integer
o int signed 32 bit integer
o long signed 64 bit integer
o float,double floating point values
www.SunilOS.com
3
Other Data Types
Reference types (composite)
o objects
o arrays
strings are supported by a built-in class named String
(java.lang.String).
string literals are supported by the language (as a special
case).
www.SunilOS.com
www.SunilOS.com 4
Custom Data Type
public class Shape {
private String color = null;
private int borderWidth = 0;
public int getBorderWidth() {
return borderWidth;
}
public void setBorderWidth(int bw){
borderWidth = bw;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
:Shape
-color :String
-borderWidth:int
+getColor():String
+setColor()
+getBorderWidth():int
+setBorderWidth()
Members
Member
variables
Member
methods
www.SunilOS.com 5
Define attribute/variable
 public class TestShape {
o public static void main(String[] args){
 Shape s; //Declaration
 s = new Shape(); //Instantiation
 s.setColor(“Red”);
 s.setBorderWidth(3);
 ….
 int borderW =s.getBorderWidth();
 System.out.println(borderW) ;
o }
 }
S is an object here
S is an instance
here
www.SunilOS.com 6
Encapsulation
Gathering all related methods and attributes in a Class
is called encapsulation.
Real World Entities – More Classes
www.SunilOS.com 7
:Automobile
-color :String
-speed:int
-make:String
+$NO_OF_GEARS
+getColor():String
+setColor()
+getMake():String
+setMake()
+break()
+changeGear()
+accelerator()
+getSpeed():int
:Person
-name:String
-dob : Date
-address:String
+$AVG_AGE
+getName():String
+setName()
+getAdress():String
+setAddress()
+getDob (): Date
+setDob ()
+getAge() : int
:Account
-number:String
-accountType : String
-balance:double
+getNumber():String
+setNumber()
+getAccountType():String
+setAccountType()
+deposit ()
+withdrawal ()
+getBalance():double
+fundTransfer()
+payBill()
www.SunilOS.com 8
Define A Class - Shape
public class Shape {
private String color = null;
private int borderWidth = 0;
public static final float PI = 3.14;
public int getBorderWidth() {
return borderWidth;
}
public void setBorderWidth(int bw) {
borderWidth = bw;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
:Shape
-color :String
-borderWidth:int
+$PI=3.14
+getColor():String
+setColor()
+getBorderWidth():int
+setBorderWidth()
Shape s1 = new Shape();
Shape s2 = new Shape();
S.o.p( s1.PI );
S.o.p( s2.PI );
S.o.p( Shape.PI );
Object initialization at the time of memory
allocation
www.SunilOS.com 9
www.SunilOS.com 10
Constructor
public class Shape {
private String color = null;
private int borderWidth = 0;
public Shape(){
System.out.println(
“This is default constructor”);
}
……………
Shape s = new Shape();
 Constructor is just like a method.
 It does not have return type.
 Its name is same as Class name.
 It is called at the time of object
instantiation (new Shape()).
 Constructors are used to initialize
instance/class variables.
 A class may have multiple
constructors with different number
of parameters.
www.SunilOS.com 11
Multiple Constructors
One class may have more than one constructors.
Multiple constructors are used to initialize different sets
of class attributes.
When a class has more than one constructors, it is called
Constructor Overloading.
Constructors those receive parameters are called
Parameterized Constructors.
www.SunilOS.com 12
Constructors Overloading
public class Shape {
private String color = null;
private int borderWidth = 0;
public Shape(){
System.out.println(“This is
default constuctor”)
}
public Shape (String c, int w){
color=c;
borderWidth=w;
}
Shape s = new Shape()
s.setColor(“Red”);
s.setBorderWidth(5);
Or
Shape s = new
Shape(“Red”,5)
Inheritance:
o Creates Specialized Classes.
 Q: Does every class have child?
 A: No, class those have specialised behaviour can have child like Shape,
Person, Automobile, Account etc. Otherwise that will be final class like Math,
String etc.
www.SunilOS.com 13
www.SunilOS.com 14
Inheritance
:Shape
color :String
borderWidth:int
getColor():String
setColor()
getBorderWidth():int
setBorderWidth()
:Rectangle
length :int
width:int
area()
getLength()
setLength()
:Circle
radius : int
area()
getRadius()
setRadius()
:Triangle
base:int
hight:int
area()
getBase()
setBase()
Circle c =new Circle();
c.getColor();
c.getBorderWidth();
c.area();
:Object
UML Notation
Inheritance
public class Shape{ .. }
public class Circle extends Shape{ .. }
public class Rectangle extends Shape{ .. }
public class Triangle extends Shape{ .. }
www.SunilOS.com 15
www.SunilOS.com 16
Method Overriding – area()
:Shape
Color :String
borderWidth:int
getColor():String
setColor()
getBorderWidth():int
setBorderWidth()
area()
:Rectangle
length :int
width:int
area()
getLength()
setLength()
:Circle
radius
area()
getRadius()
setRadius()
:Triangle
base:int
hight:int
area()
getBase()
setBase()
Shape s= new Circle();
s.getColor();
s.getBorderWidth();
s.area();
Polymorphism - Method Overriding
 public class Shape{
 public double area(){}
 }
 public class Circle extends Shape{
 public double area(){ ……..}
 }
 public class Rectangle extends Shape{
 public double area(){……..}
 }
 public class Triangle extends Shape{
 public double area(){……..}
 }
In Method Overriding
o Method signature
(name, return
type,no of
parameters) should
be same
o Applies only in case
of Inheritance
www.SunilOS.com 17
Polymorphism - Method Overloading
 public class Addition{
 public void sum(int a, int b){..}
 public void sum(int a, int b, int c){..}
 public double sum(double a, int b){..}
 public float sum(float a, int b){..}
 }
In Overloading
o Method name should be
same
o Method Parameter should
be different in no and type
both
o Return type can be same or
different
www.SunilOS.com 18
www.SunilOS.com 19
Data Abstration – Abstract Class
public abstract class Shape {
public abstract double area();
}
public class Circle extends Shape{
public double area(){
double circleArea = 3.14*radius*radius;
return circleArea;
}
public static void main(){
Shape s = new Circle();
double area = s.area();
System.out.println(area);
}
}
www.SunilOS.com 20
Data Abstraction - interface Richman
public interface Richman {
o public void earnMoney();
o public void donation();
o public void party();
}
www.SunilOS.com 21
interface SocialWorker
public interface SocialWorker{
o public void helpToOthers();
}
www.SunilOS.com 22
interface SocialWorker
 public class Businessman extends Person implements Richman,
SocialWorker {
 public void donation() {..}
 public void earnMoney() {..}
 public void party() {..}
 public void helpToOthers() {..}
 }
www.SunilOS.com 23
Explicit constructor calling using this()
 public Person() {
 System.out.println("Person Default Con");
 }
 public Person(String fn, String ln) {
o firstName = fn;
o lastName = ln;
o System.out.println("2 params constructor is called");
 }
 public Person(String fn, String ln, String address) {
1. this(fn,ln) ;
2. this.address = address;
3. System.out.println(“3 params constructor is called");
 }
www.SunilOS.com 24
Explicit constructor calling using super()
 public class Employee extends Person {
 private String designation = null;
 public Employee() {
 System.out.println("Default Constructor");
 }
 public Employee(String fn, String ln, String des) {
 super(fn, ln);
 designation = des;
 System.out.println(“3 params constructor is called");
 }
www.SunilOS.com 25
Method Definition
public class Shape {
..
public void setBorderWidth(int bw)
{
borderWidth = bw;
}
}
Method
Default Constructor
 Default constructor does not receive any parameter.
o public Shape(){ .. }
 If User does not define any constructor then Default Constructor
will be created by Java Compiler.
 But if user defines single or multiple constructors then default
constructor will not be generated by Java Compiler.
www.SunilOS.com 26
www.SunilOS.com 27
Abstract Class
 What code can be written in Shape.area() method?
o Nothing, area() method is defined by child classes. It should have only
declaration.
 Is Shape a concrete class?
o NO, Rectangle, Circle and Triangle are concrete classes.
 If it has only area declaration then
o Method will be abstract and class will be abstract as well.
 Benefit?
o Parent will enforce child to implement area() method.
o Child has to mandatorily define (implement) area method.
o This will achieve polymorphism.
www.SunilOS.com 28
Interface
When all methods are abstract then interface is created.
It has abstract methods and constants.
It represents a role (abstract view) for a class.
One interface can extend another interface using extends
keyword.
One Class can implement multiple interfaces using
implements keyword.
www.SunilOS.com 29
Interfaces
Richman
earnMony()
donation()
party()
Businessman
name
address
earnMony()
donation()
party()
Richman rm = new Businessman();
SocialWorker sw = new Businessman();
Businessman bm = new Businessman();
SocialWorker
helpToOthers()
Businessman
name
address
earnMony()
donation()
party()
helpToOthers()
Businessman
name
address
helpToOthers()
www.SunilOS.com 30
Interface
It declares APIs.
Specifications are defined as interfaces.
o JDBC
o Collection
o EJB
o JNI
o etc.
Data Hiding ( Cont. )
Data Hiding is an aspect of Object Oriented
Programming (OOP) that allows developers to
protect private data and hide implementation
details.
Developers can hide class members from other
classes. Access of class members can be
restricted or hide with the help of access
modifiers.
www.SunilOS.com 31
www.SunilOS.com 32
How a constructor can call another constructor ?
 public class Person {
 protected String firstName = null;
 protected String lastName = null;
 protected String address = null;
 public Person() {
 System.out.println("Person Default Con");
 }
 public Person(String fn, String ln) {
o firstName = fn;
o lastName = ln;
o System.out.println(“2 params constructor is called");
 }
www.SunilOS.com 33
How a constructor can call another constructor ?
 public Person(String fn, String ln, String address) {
o firstName = fn;
o lastName = ln;
o this.address = address;
o System.out.println(“3 params constructor is called");
 }
www.SunilOS.com 34
Super default constructor
 If Child constructor does not call parent’s constructor then Parent’s default
constructor is automatically called.
 public Employee() {
 System.out.println("Default Constructor");
 }
 Is Equal to
 public Employee() {
 super();
 System.out.println("Default Constructor");
 }
www.SunilOS.com 35
Super default constructor (cont.)
 public Employee(String fn, String ln, String des) {
o designation = des;
o System.out.println(“3 params constructor is called ");
 }
 Is Equal to
 public Employee(String fn, String ln, String des) {
o super();
o designation = des;
o System.out.println(“3 params constructor is called");
 }
www.SunilOS.com 36
How to call Parent’s overridden method?
 public class Person {
 public void changeAddress() {
o System.out.println("Person change Address");
 }
 …
 public class Employee extends Person {
 public void changeAddress() {
o System.out.println("*****");
o super.changeAddress();
o System.out.println("Employee change Address");
 }
 …
www.SunilOS.com 37
Interesting facts - Overriding
 public class Account{
o public int getAmount() {
o return 5;
o }
 }
 public class SavingAccount extends Account {
o public int getAmount() {
 return 10;
o }
 }
www.SunilOS.com 38
What is Output Of
 public class Test {
 public static void main(String[] args) {
o SavingAccount s = new SavingAccount ();
o Account a = new Account ();
o Account sa = new SavingAccount ();
o System.out.println(s.getAmount());
o System.out.println(a.getAmount());
o System.out.println(sa.getAmount());
o }
 }
Interesting facts - Overriding
 public class Account{
o public int getAmount() {
o return 5;
o }
 }
 public class SavingAccount extends Account{
o public int getAmount() {
 int i = super.getAmount() + 10;
 return i;
o }
 }
www.SunilOS.com 39
www.SunilOS.com 40
Polymorphism
 Three Common Uses of Polymorphism:
o Using Polymorphism in Arrays.
o Using Polymorphism for Method Arguments.
o Using Polymorphism for Method Return Type.
 Ways to Provide polymorphism:
o Through Interfaces.
o Method Overriding.
o Method Overloading.
www.SunilOS.com 41
1) Using Polymorphism in Arrays
Shape s[] = new Shape[3];
s[0] = new Rectangle()
s[1] = new Circle()
s[2] = new Triangle()
s[0]:Rectangle
color
borderWidth
length = 17
Width = 35
s[1]:Circle
color
borderWidth
radius = 11
s[2]:Triangle
color
borderWidth
base:15
hight:7
www.SunilOS.com 42
2) Using Polymorphism for Method Arguments
public static void main(String[] args) {
Shape[] s = new Shape[3];
s[0] = new Rectangle();
s[1] = new Circle();
s[2] = new Triangle();
double totalArea = calcArea(s);
System.out.println(totalArea);
}
public static double calcArea(Shape[] s) {
double totalArea = 0;
for(int i =0;i<s.length; i++){
totalArea += s[i].area();
}
return totalArea;
}
*The method overriding is an example of runtime polymorphism.
www.SunilOS.com 43
3) Polymorphism using Return Type
public static Shape getShape(int i) {
if (i == 1) return new Rectangle();
if (i == 2) return new Circle();
if (i == 3) return new Triangle();
}
www.SunilOS.com 44
Method Overloading
PrintWriter
o println(String)
o println(int)
o println(double)
o println(boolean)
o println()
www.SunilOS.com 45
The Final modifier
Class :
o Final classes can not have Children.
o public final class Math
Method:
o Final Methods can not be overridden.
o public final double sqrt(int i);
Attribute:
o Final attributes can be assigned a value once in a life.
o public final float PI = 3.14;
Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 46
Thank You!
www.SunilOS.com 47
www.SunilOS.com

More Related Content

What's hot (20)

PPT
Collections Framework
Sunil OS
 
PPT
Java 8 - CJ
Sunil OS
 
PPT
Java Basics
Sunil OS
 
PPT
DJango
Sunil OS
 
PPTX
Templates in C++
Tech_MX
 
PPT
JDBC
Sunil OS
 
DOCX
Basic Programs of C++
Bharat Kalia
 
PPT
Log4 J
Sunil OS
 
PPTX
11. Java Objects and classes
Intro C# Book
 
PPT
Java Threads and Concurrency
Sunil OS
 
PPT
JavaScript
Sunil OS
 
PPT
Jsp/Servlet
Sunil OS
 
PPT
C Basics
Sunil OS
 
PPT
JAVA Variables and Operators
Sunil OS
 
PPT
Resource Bundle
Sunil OS
 
PPTX
Программирование на языке C Sharp (СИ решетка)
Alexandr Konfidentsialno
 
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
PPT
PDBC
Sunil OS
 
PPT
Functions in C++
Mohammed Sikander
 
PPTX
Object Oriented Programming with C#
foreverredpb
 
Collections Framework
Sunil OS
 
Java 8 - CJ
Sunil OS
 
Java Basics
Sunil OS
 
DJango
Sunil OS
 
Templates in C++
Tech_MX
 
JDBC
Sunil OS
 
Basic Programs of C++
Bharat Kalia
 
Log4 J
Sunil OS
 
11. Java Objects and classes
Intro C# Book
 
Java Threads and Concurrency
Sunil OS
 
JavaScript
Sunil OS
 
Jsp/Servlet
Sunil OS
 
C Basics
Sunil OS
 
JAVA Variables and Operators
Sunil OS
 
Resource Bundle
Sunil OS
 
Программирование на языке C Sharp (СИ решетка)
Alexandr Konfidentsialno
 
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
PDBC
Sunil OS
 
Functions in C++
Mohammed Sikander
 
Object Oriented Programming with C#
foreverredpb
 

Similar to OOP Core Concept (20)

PPT
OOP v3
Sunil OS
 
PPT
C++ oop
Sunil OS
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
PDF
Core java complete notes - PAID call at +91-814-614-5674
WebKrit Infocom
 
PDF
JAVA-PPT'S.pdf
AnmolVerma363503
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
PPT
Oops Concept Java
Kamlesh Singh
 
DOCX
Object Oriented Programming All Unit Notes
BalamuruganV28
 
PPT
Object and class
mohit tripathi
 
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
PPTX
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
PPTX
Android Training (Java Review)
Khaled Anaqwa
 
PPTX
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
PPTX
JAVA-PPT'S.pptx
RaazIndia
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Oop ppt
Shani Manjara
 
PDF
Dive into Object Orienter Programming and Design Patterns through java
Damith Warnakulasuriya
 
PPTX
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
OOP v3
Sunil OS
 
C++ oop
Sunil OS
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Core java complete notes - PAID call at +91-814-614-5674
WebKrit Infocom
 
JAVA-PPT'S.pdf
AnmolVerma363503
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Pi j3.2 polymorphism
mcollison
 
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Oops Concept Java
Kamlesh Singh
 
Object Oriented Programming All Unit Notes
BalamuruganV28
 
Object and class
mohit tripathi
 
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Android Training (Java Review)
Khaled Anaqwa
 
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
RaazIndia
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Oop ppt
Shani Manjara
 
Dive into Object Orienter Programming and Design Patterns through java
Damith Warnakulasuriya
 
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Ad

More from Rays Technologies (6)

PPT
JSP/Servlet Core Concept
Rays Technologies
 
PPT
JDBC Core Concept
Rays Technologies
 
PPT
SQL Core Concept
Rays Technologies
 
PPT
Networking Core Concept
Rays Technologies
 
PPT
Collection Core Concept
Rays Technologies
 
PPT
Initial Java Core Concept
Rays Technologies
 
JSP/Servlet Core Concept
Rays Technologies
 
JDBC Core Concept
Rays Technologies
 
SQL Core Concept
Rays Technologies
 
Networking Core Concept
Rays Technologies
 
Collection Core Concept
Rays Technologies
 
Initial Java Core Concept
Rays Technologies
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 

OOP Core Concept

  • 1. www.SunilOS.com 1 Object Oriented Programming OOP……. Not OOPS!! Core Concepts www.sunilos.com www.raystec.com
  • 2. 2 Java Primitive Data Types Primitive Data Types: o boolean true or false o char unicode (16 bits) o byte signed 8 bit integer o short signed 16 bit integer o int signed 32 bit integer o long signed 64 bit integer o float,double floating point values www.SunilOS.com
  • 3. 3 Other Data Types Reference types (composite) o objects o arrays strings are supported by a built-in class named String (java.lang.String). string literals are supported by the language (as a special case). www.SunilOS.com
  • 4. www.SunilOS.com 4 Custom Data Type public class Shape { private String color = null; private int borderWidth = 0; public int getBorderWidth() { return borderWidth; } public void setBorderWidth(int bw){ borderWidth = bw; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } :Shape -color :String -borderWidth:int +getColor():String +setColor() +getBorderWidth():int +setBorderWidth() Members Member variables Member methods
  • 5. www.SunilOS.com 5 Define attribute/variable  public class TestShape { o public static void main(String[] args){  Shape s; //Declaration  s = new Shape(); //Instantiation  s.setColor(“Red”);  s.setBorderWidth(3);  ….  int borderW =s.getBorderWidth();  System.out.println(borderW) ; o }  } S is an object here S is an instance here
  • 6. www.SunilOS.com 6 Encapsulation Gathering all related methods and attributes in a Class is called encapsulation.
  • 7. Real World Entities – More Classes www.SunilOS.com 7 :Automobile -color :String -speed:int -make:String +$NO_OF_GEARS +getColor():String +setColor() +getMake():String +setMake() +break() +changeGear() +accelerator() +getSpeed():int :Person -name:String -dob : Date -address:String +$AVG_AGE +getName():String +setName() +getAdress():String +setAddress() +getDob (): Date +setDob () +getAge() : int :Account -number:String -accountType : String -balance:double +getNumber():String +setNumber() +getAccountType():String +setAccountType() +deposit () +withdrawal () +getBalance():double +fundTransfer() +payBill()
  • 8. www.SunilOS.com 8 Define A Class - Shape public class Shape { private String color = null; private int borderWidth = 0; public static final float PI = 3.14; public int getBorderWidth() { return borderWidth; } public void setBorderWidth(int bw) { borderWidth = bw; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } :Shape -color :String -borderWidth:int +$PI=3.14 +getColor():String +setColor() +getBorderWidth():int +setBorderWidth() Shape s1 = new Shape(); Shape s2 = new Shape(); S.o.p( s1.PI ); S.o.p( s2.PI ); S.o.p( Shape.PI );
  • 9. Object initialization at the time of memory allocation www.SunilOS.com 9
  • 10. www.SunilOS.com 10 Constructor public class Shape { private String color = null; private int borderWidth = 0; public Shape(){ System.out.println( “This is default constructor”); } …………… Shape s = new Shape();  Constructor is just like a method.  It does not have return type.  Its name is same as Class name.  It is called at the time of object instantiation (new Shape()).  Constructors are used to initialize instance/class variables.  A class may have multiple constructors with different number of parameters.
  • 11. www.SunilOS.com 11 Multiple Constructors One class may have more than one constructors. Multiple constructors are used to initialize different sets of class attributes. When a class has more than one constructors, it is called Constructor Overloading. Constructors those receive parameters are called Parameterized Constructors.
  • 12. www.SunilOS.com 12 Constructors Overloading public class Shape { private String color = null; private int borderWidth = 0; public Shape(){ System.out.println(“This is default constuctor”) } public Shape (String c, int w){ color=c; borderWidth=w; } Shape s = new Shape() s.setColor(“Red”); s.setBorderWidth(5); Or Shape s = new Shape(“Red”,5)
  • 13. Inheritance: o Creates Specialized Classes.  Q: Does every class have child?  A: No, class those have specialised behaviour can have child like Shape, Person, Automobile, Account etc. Otherwise that will be final class like Math, String etc. www.SunilOS.com 13
  • 14. www.SunilOS.com 14 Inheritance :Shape color :String borderWidth:int getColor():String setColor() getBorderWidth():int setBorderWidth() :Rectangle length :int width:int area() getLength() setLength() :Circle radius : int area() getRadius() setRadius() :Triangle base:int hight:int area() getBase() setBase() Circle c =new Circle(); c.getColor(); c.getBorderWidth(); c.area(); :Object UML Notation
  • 15. Inheritance public class Shape{ .. } public class Circle extends Shape{ .. } public class Rectangle extends Shape{ .. } public class Triangle extends Shape{ .. } www.SunilOS.com 15
  • 16. www.SunilOS.com 16 Method Overriding – area() :Shape Color :String borderWidth:int getColor():String setColor() getBorderWidth():int setBorderWidth() area() :Rectangle length :int width:int area() getLength() setLength() :Circle radius area() getRadius() setRadius() :Triangle base:int hight:int area() getBase() setBase() Shape s= new Circle(); s.getColor(); s.getBorderWidth(); s.area();
  • 17. Polymorphism - Method Overriding  public class Shape{  public double area(){}  }  public class Circle extends Shape{  public double area(){ ……..}  }  public class Rectangle extends Shape{  public double area(){……..}  }  public class Triangle extends Shape{  public double area(){……..}  } In Method Overriding o Method signature (name, return type,no of parameters) should be same o Applies only in case of Inheritance www.SunilOS.com 17
  • 18. Polymorphism - Method Overloading  public class Addition{  public void sum(int a, int b){..}  public void sum(int a, int b, int c){..}  public double sum(double a, int b){..}  public float sum(float a, int b){..}  } In Overloading o Method name should be same o Method Parameter should be different in no and type both o Return type can be same or different www.SunilOS.com 18
  • 19. www.SunilOS.com 19 Data Abstration – Abstract Class public abstract class Shape { public abstract double area(); } public class Circle extends Shape{ public double area(){ double circleArea = 3.14*radius*radius; return circleArea; } public static void main(){ Shape s = new Circle(); double area = s.area(); System.out.println(area); } }
  • 20. www.SunilOS.com 20 Data Abstraction - interface Richman public interface Richman { o public void earnMoney(); o public void donation(); o public void party(); }
  • 21. www.SunilOS.com 21 interface SocialWorker public interface SocialWorker{ o public void helpToOthers(); }
  • 22. www.SunilOS.com 22 interface SocialWorker  public class Businessman extends Person implements Richman, SocialWorker {  public void donation() {..}  public void earnMoney() {..}  public void party() {..}  public void helpToOthers() {..}  }
  • 23. www.SunilOS.com 23 Explicit constructor calling using this()  public Person() {  System.out.println("Person Default Con");  }  public Person(String fn, String ln) { o firstName = fn; o lastName = ln; o System.out.println("2 params constructor is called");  }  public Person(String fn, String ln, String address) { 1. this(fn,ln) ; 2. this.address = address; 3. System.out.println(“3 params constructor is called");  }
  • 24. www.SunilOS.com 24 Explicit constructor calling using super()  public class Employee extends Person {  private String designation = null;  public Employee() {  System.out.println("Default Constructor");  }  public Employee(String fn, String ln, String des) {  super(fn, ln);  designation = des;  System.out.println(“3 params constructor is called");  }
  • 25. www.SunilOS.com 25 Method Definition public class Shape { .. public void setBorderWidth(int bw) { borderWidth = bw; } } Method
  • 26. Default Constructor  Default constructor does not receive any parameter. o public Shape(){ .. }  If User does not define any constructor then Default Constructor will be created by Java Compiler.  But if user defines single or multiple constructors then default constructor will not be generated by Java Compiler. www.SunilOS.com 26
  • 27. www.SunilOS.com 27 Abstract Class  What code can be written in Shape.area() method? o Nothing, area() method is defined by child classes. It should have only declaration.  Is Shape a concrete class? o NO, Rectangle, Circle and Triangle are concrete classes.  If it has only area declaration then o Method will be abstract and class will be abstract as well.  Benefit? o Parent will enforce child to implement area() method. o Child has to mandatorily define (implement) area method. o This will achieve polymorphism.
  • 28. www.SunilOS.com 28 Interface When all methods are abstract then interface is created. It has abstract methods and constants. It represents a role (abstract view) for a class. One interface can extend another interface using extends keyword. One Class can implement multiple interfaces using implements keyword.
  • 29. www.SunilOS.com 29 Interfaces Richman earnMony() donation() party() Businessman name address earnMony() donation() party() Richman rm = new Businessman(); SocialWorker sw = new Businessman(); Businessman bm = new Businessman(); SocialWorker helpToOthers() Businessman name address earnMony() donation() party() helpToOthers() Businessman name address helpToOthers()
  • 30. www.SunilOS.com 30 Interface It declares APIs. Specifications are defined as interfaces. o JDBC o Collection o EJB o JNI o etc.
  • 31. Data Hiding ( Cont. ) Data Hiding is an aspect of Object Oriented Programming (OOP) that allows developers to protect private data and hide implementation details. Developers can hide class members from other classes. Access of class members can be restricted or hide with the help of access modifiers. www.SunilOS.com 31
  • 32. www.SunilOS.com 32 How a constructor can call another constructor ?  public class Person {  protected String firstName = null;  protected String lastName = null;  protected String address = null;  public Person() {  System.out.println("Person Default Con");  }  public Person(String fn, String ln) { o firstName = fn; o lastName = ln; o System.out.println(“2 params constructor is called");  }
  • 33. www.SunilOS.com 33 How a constructor can call another constructor ?  public Person(String fn, String ln, String address) { o firstName = fn; o lastName = ln; o this.address = address; o System.out.println(“3 params constructor is called");  }
  • 34. www.SunilOS.com 34 Super default constructor  If Child constructor does not call parent’s constructor then Parent’s default constructor is automatically called.  public Employee() {  System.out.println("Default Constructor");  }  Is Equal to  public Employee() {  super();  System.out.println("Default Constructor");  }
  • 35. www.SunilOS.com 35 Super default constructor (cont.)  public Employee(String fn, String ln, String des) { o designation = des; o System.out.println(“3 params constructor is called ");  }  Is Equal to  public Employee(String fn, String ln, String des) { o super(); o designation = des; o System.out.println(“3 params constructor is called");  }
  • 36. www.SunilOS.com 36 How to call Parent’s overridden method?  public class Person {  public void changeAddress() { o System.out.println("Person change Address");  }  …  public class Employee extends Person {  public void changeAddress() { o System.out.println("*****"); o super.changeAddress(); o System.out.println("Employee change Address");  }  …
  • 37. www.SunilOS.com 37 Interesting facts - Overriding  public class Account{ o public int getAmount() { o return 5; o }  }  public class SavingAccount extends Account { o public int getAmount() {  return 10; o }  }
  • 38. www.SunilOS.com 38 What is Output Of  public class Test {  public static void main(String[] args) { o SavingAccount s = new SavingAccount (); o Account a = new Account (); o Account sa = new SavingAccount (); o System.out.println(s.getAmount()); o System.out.println(a.getAmount()); o System.out.println(sa.getAmount()); o }  }
  • 39. Interesting facts - Overriding  public class Account{ o public int getAmount() { o return 5; o }  }  public class SavingAccount extends Account{ o public int getAmount() {  int i = super.getAmount() + 10;  return i; o }  } www.SunilOS.com 39
  • 40. www.SunilOS.com 40 Polymorphism  Three Common Uses of Polymorphism: o Using Polymorphism in Arrays. o Using Polymorphism for Method Arguments. o Using Polymorphism for Method Return Type.  Ways to Provide polymorphism: o Through Interfaces. o Method Overriding. o Method Overloading.
  • 41. www.SunilOS.com 41 1) Using Polymorphism in Arrays Shape s[] = new Shape[3]; s[0] = new Rectangle() s[1] = new Circle() s[2] = new Triangle() s[0]:Rectangle color borderWidth length = 17 Width = 35 s[1]:Circle color borderWidth radius = 11 s[2]:Triangle color borderWidth base:15 hight:7
  • 42. www.SunilOS.com 42 2) Using Polymorphism for Method Arguments public static void main(String[] args) { Shape[] s = new Shape[3]; s[0] = new Rectangle(); s[1] = new Circle(); s[2] = new Triangle(); double totalArea = calcArea(s); System.out.println(totalArea); } public static double calcArea(Shape[] s) { double totalArea = 0; for(int i =0;i<s.length; i++){ totalArea += s[i].area(); } return totalArea; } *The method overriding is an example of runtime polymorphism.
  • 43. www.SunilOS.com 43 3) Polymorphism using Return Type public static Shape getShape(int i) { if (i == 1) return new Rectangle(); if (i == 2) return new Circle(); if (i == 3) return new Triangle(); }
  • 44. www.SunilOS.com 44 Method Overloading PrintWriter o println(String) o println(int) o println(double) o println(boolean) o println()
  • 45. www.SunilOS.com 45 The Final modifier Class : o Final classes can not have Children. o public final class Math Method: o Final Methods can not be overridden. o public final double sqrt(int i); Attribute: o Final attributes can be assigned a value once in a life. o public final float PI = 3.14;
  • 46. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 46

Editor's Notes

  • #2: www.sunilos.com
  • #3: www.sunrays.co.in
  • #4: www.sunrays.co.in
  • #5: www.sunrays.co.in
  • #6: www.sunrays.co.in
  • #7: www.sunrays.co.in
  • #9: www.sunrays.co.in
  • #11: www.sunrays.co.in
  • #12: www.sunrays.co.in
  • #13: www.sunrays.co.in
  • #15: www.sunrays.co.in
  • #17: www.sunrays.co.in
  • #20: www.sunrays.co.in
  • #21: www.sunrays.co.in
  • #22: www.sunrays.co.in
  • #23: www.sunrays.co.in
  • #24: www.sunrays.co.in
  • #25: www.sunrays.co.in
  • #26: www.sunrays.co.in
  • #28: www.sunrays.co.in
  • #29: www.sunrays.co.in
  • #30: www.sunrays.co.in
  • #31: www.sunrays.co.in
  • #33: www.sunrays.co.in
  • #34: www.sunrays.co.in
  • #35: www.sunrays.co.in
  • #36: www.sunrays.co.in
  • #37: www.sunrays.co.in
  • #38: www.sunrays.co.in
  • #39: www.sunrays.co.in
  • #40: www.sunrays.co.in
  • #41: www.sunrays.co.in
  • #42: www.sunrays.co.in
  • #43: www.sunrays.co.in
  • #44: www.sunrays.co.in
  • #45: www.sunrays.co.in
  • #46: www.sunrays.co.in