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)

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

Recently uploaded (20)

Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
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 } }