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)

PDF
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
PDF
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
tabbu23
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
‫Chapter3 inheritance
Mahmoud Alfarra
 
PDF
Lec 5 13_aug [compatibility mode]
Palak Sanghani
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PPT
Java oops PPT
kishu0005
 
PPT
Java class
Arati Gadgil
 
PPTX
UNIT 2 & unit 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
PPTX
class object.pptx
Killmekhilati
 
DOCX
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
PPT
Object Oriented Programming with Java
backdoor
 
PPTX
Inheritance
Jancirani Selvam
 
PPTX
Inheritance Slides
Ahsan Raja
 
PPS
Introduction to class in java
kamal kotecha
 
PDF
Java unit2
Abhishek Khune
 
PPT
Sonu wiziq
Sonu WIZIQ
 
DOCX
Keyword of java
Jani Harsh
 
PPTX
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
PDF
Object Oriented Programming - 5. Class & Object
AndiNurkholis1
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
tabbu23
 
Inheritance & interface ppt Inheritance
narikamalliy
 
‫Chapter3 inheritance
Mahmoud Alfarra
 
Lec 5 13_aug [compatibility mode]
Palak Sanghani
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
Java oops PPT
kishu0005
 
Java class
Arati Gadgil
 
UNIT 2 & unit 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
class object.pptx
Killmekhilati
 
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Object Oriented Programming with Java
backdoor
 
Inheritance
Jancirani Selvam
 
Inheritance Slides
Ahsan Raja
 
Introduction to class in java
kamal kotecha
 
Java unit2
Abhishek Khune
 
Sonu wiziq
Sonu WIZIQ
 
Keyword of java
Jani Harsh
 
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
Object Oriented Programming - 5. Class & Object
AndiNurkholis1
 

Recently uploaded (20)

PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Français Patch Tuesday - Juillet
Ivanti
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
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 } }