SlideShare a Scribd company logo
Unit – 02
object oriented approach using JAVA
Class and objects
class is a template for an object
an object is an instance of a class
Creating class:
class classname {
type instance-variable1;
Type instance-variable2; // ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
} // ...
type methodnameN(parameter-list) {
// body of method
}
}
Creating object :
class-var = new classname ( );
Continued…
//creating class
class s2 {
int a=10;
void num(){
System.out.println(a);
}
public static void main(String agrs[]){
s2 object = new s2(); //creating object
object.num();
}
}
Class and objects
Abstration
 Abstraction in Java is the process of hiding the implementation details and
only showing the essential functionality or features to the user.
 Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details.
 In Java, abstraction is achieved by interfaces and abstract classes.
 Using abstract class will only help us to acheive partially abstraction.
 We can achieve 100% abstraction using interfaces.
(will study later with coding example)
Polymorphism
one name many forms. We can achieve polymorphism by changing the no. of parameters of method or
by changing the datatype of method
Types of Java Polymorphism
 Compile-Time Polymorphism (method overloading)
 Runtime Polymorphism (method overriding)
Compile-time polymorphism (method overloading)
public class Main{
void add(){ //method overloading
int a=30,b=20;
System.out.println((a+b));
}
void add(int a , int b){//method overloading
System.out.println((a+b));
}
public static void main(String[] args) {
Main ob= new Main();
ob.add();
ob.add(10,20);
}}
Run-time polymorphism (method overriding)
class mca_1st_year {
void add(){
System.out.println("super class");
}
}
public class a extends mca_1st_year{ //inheritence
void add(){
//super.add(); // for calling parent class
method
System.out.println("sub class");
}
public static void main(String args[]){
a obj = new a();
obj.add();
}
}
Inheritance
acquiring the properties of a class is called inheritance.
The class that is inherited is called a superclass.
The class that does the inheriting is called a subclass.
For inheriting the property extends keyword is used.
class mca_1st_year {
void add(){
System.out.println("super class");
}
}
public class a extends mca_1st_year{ //inheritance
void add(){
//super.add(); // for calling parent class method
System.out.println("sub class");
}
public static void main(String args[]){
a obj = new a();
obj.add();
}
}
Inheritance
Types of inheritance:
 Single inheritance
 Multilevel inheritance
 Hierarchal inheritance
 Multiple inheritance (not supported in java)
Try coding example of each…
Encapsulation
Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.
Encapsulation is achieved by using different access modifiers :
 public
 private
 protected
 default (no modifier)
Encapsulation continued…
private int a;
int b;
protected int c;
public int d;
Try yourself in labs….
Private access modifier
How to create our own package (user
defined package) :
 package is collection of classes.
 package keyword is used.(eg. package “mca_1”)
 For importing user defined package:
import packageName.className; //syntax
import mca_1.classname;
We will study packages in next unit….
Methods
a block of code that runs only when it is called.
type name(parameter-list) //syntax
{
// body of method
}
Calling a method :
Box b= new Box();
b.volume();
b.setDim(1.0,1.0,2.0);
∞ Method overloading – same name with different parameters
within same class (ex. compile time Polymorphism)
∞ Method overriding – same name same parameters but in different
classes (ex. Inheritence i.e. run time polymorphism)
Constructors
special type of methods whose name is same as the class
name and have no return type.
The constructor is automatically called when the object is created
Types of constructor :
 Default constructor
 Parameterised constructor
 Copy constructor
Default constructor
class a{
a(){
System.out.println("default constructor");
}
}
public class cons {
public static void main(String arg[]){
a obj = new a();
}
}
Parameterised constructor
class a{
a(int a , int b){
System.out.println("parameterised constructor " + (a+b));
}
}
public class cons {
public static void main(String arg[]){
a obj1;
obj1 = new a(10,30);
}
}
Copy constructor
Import java.io.*;
class a{
String s;
int age;
a(String s , int age){
this.s= s;
this.age=age;
}
a(a ob){
this.s=ob.s;
this.age=ob.age;
System.out.println("copy constructor :"+s+" age :"+age);
}
}
public class cons {
public static void main(String arg[]){
a obj3=new a("sukanya", 26);
a obj4= new a(obj3); //copy
}
}

More Related Content

Similar to introduction to object oriented programming language java (20)

PPTX
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPTX
L03 Software Design
Ólafur Andri Ragnarsson
 
PPT
Oop java
Minal Maniar
 
PPSX
Oop features java presentationshow
ilias ahmed
 
PPT
Java Tutorials
Woxa Technologies
 
DOCX
javaopps concepts
Nikhil Agrawal
 
PPTX
Android Training (Java Review)
Khaled Anaqwa
 
PDF
Second chapter-java
Ahmad sohail Kakar
 
PDF
12th ip CBSE chapter 4 oop in java notes complete
Harish Gyanani
 
PPT
java training faridabad
Woxa Technologies
 
PPTX
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
Object Oriented Programming Concepts
SanmatiRM
 
PPTX
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
UNIT_-II_2021R.pptx
RDeepa9
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PDF
OOPs Concepts - Android Programming
Purvik Rana
 
PPTX
Introduction to oop and java fundamentals
AnsgarMary
 
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
L03 Software Design
Ólafur Andri Ragnarsson
 
Oop java
Minal Maniar
 
Oop features java presentationshow
ilias ahmed
 
Java Tutorials
Woxa Technologies
 
javaopps concepts
Nikhil Agrawal
 
Android Training (Java Review)
Khaled Anaqwa
 
Second chapter-java
Ahmad sohail Kakar
 
12th ip CBSE chapter 4 oop in java notes complete
Harish Gyanani
 
java training faridabad
Woxa Technologies
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
Object Oriented Programming Concepts
SanmatiRM
 
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
UNIT_-II_2021R.pptx
RDeepa9
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
OOPs Concepts - Android Programming
Purvik Rana
 
Introduction to oop and java fundamentals
AnsgarMary
 

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Biography of Daniel Podor.pdf
Daniel Podor
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Ad

introduction to object oriented programming language java

  • 1. Unit – 02 object oriented approach using JAVA
  • 2. Class and objects class is a template for an object an object is an instance of a class Creating class: class classname { type instance-variable1; Type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of method } } Creating object : class-var = new classname ( ); Continued…
  • 3. //creating class class s2 { int a=10; void num(){ System.out.println(a); } public static void main(String agrs[]){ s2 object = new s2(); //creating object object.num(); } } Class and objects
  • 4. Abstration  Abstraction in Java is the process of hiding the implementation details and only showing the essential functionality or features to the user.  Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.  In Java, abstraction is achieved by interfaces and abstract classes.  Using abstract class will only help us to acheive partially abstraction.  We can achieve 100% abstraction using interfaces. (will study later with coding example)
  • 5. Polymorphism one name many forms. We can achieve polymorphism by changing the no. of parameters of method or by changing the datatype of method Types of Java Polymorphism  Compile-Time Polymorphism (method overloading)  Runtime Polymorphism (method overriding)
  • 6. Compile-time polymorphism (method overloading) public class Main{ void add(){ //method overloading int a=30,b=20; System.out.println((a+b)); } void add(int a , int b){//method overloading System.out.println((a+b)); } public static void main(String[] args) { Main ob= new Main(); ob.add(); ob.add(10,20); }}
  • 7. Run-time polymorphism (method overriding) class mca_1st_year { void add(){ System.out.println("super class"); } } public class a extends mca_1st_year{ //inheritence void add(){ //super.add(); // for calling parent class method System.out.println("sub class"); } public static void main(String args[]){ a obj = new a(); obj.add(); } }
  • 8. Inheritance acquiring the properties of a class is called inheritance. The class that is inherited is called a superclass. The class that does the inheriting is called a subclass. For inheriting the property extends keyword is used. class mca_1st_year { void add(){ System.out.println("super class"); } } public class a extends mca_1st_year{ //inheritance void add(){ //super.add(); // for calling parent class method System.out.println("sub class"); } public static void main(String args[]){ a obj = new a(); obj.add(); } }
  • 9. Inheritance Types of inheritance:  Single inheritance  Multilevel inheritance  Hierarchal inheritance  Multiple inheritance (not supported in java) Try coding example of each…
  • 10. Encapsulation Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Encapsulation is achieved by using different access modifiers :  public  private  protected  default (no modifier)
  • 11. Encapsulation continued… private int a; int b; protected int c; public int d; Try yourself in labs….
  • 13. How to create our own package (user defined package) :  package is collection of classes.  package keyword is used.(eg. package “mca_1”)  For importing user defined package: import packageName.className; //syntax import mca_1.classname; We will study packages in next unit….
  • 14. Methods a block of code that runs only when it is called. type name(parameter-list) //syntax { // body of method }
  • 15. Calling a method : Box b= new Box(); b.volume(); b.setDim(1.0,1.0,2.0); ∞ Method overloading – same name with different parameters within same class (ex. compile time Polymorphism) ∞ Method overriding – same name same parameters but in different classes (ex. Inheritence i.e. run time polymorphism)
  • 16. Constructors special type of methods whose name is same as the class name and have no return type. The constructor is automatically called when the object is created Types of constructor :  Default constructor  Parameterised constructor  Copy constructor
  • 17. Default constructor class a{ a(){ System.out.println("default constructor"); } } public class cons { public static void main(String arg[]){ a obj = new a(); } }
  • 18. Parameterised constructor class a{ a(int a , int b){ System.out.println("parameterised constructor " + (a+b)); } } public class cons { public static void main(String arg[]){ a obj1; obj1 = new a(10,30); } }
  • 19. Copy constructor Import java.io.*; class a{ String s; int age; a(String s , int age){ this.s= s; this.age=age; } a(a ob){ this.s=ob.s; this.age=ob.age; System.out.println("copy constructor :"+s+" age :"+age); } } public class cons { public static void main(String arg[]){ a obj3=new a("sukanya", 26); a obj4= new a(obj3); //copy } }