SlideShare a Scribd company logo
1 of 14
Download to read offline
Introduction to Java
Lecture 6
Naveen Kumar
Class definition
class A
{
int i;
char ch;
void set()
{…}
int get(int b)
{…}
}
Method Declarations
 General format of method declaration:
Modifier return-type method-name( parameter1, …, parameterN )
{
body (declarations and statements);
}
 Modifiers—such as public, private, and others you will learn later.
 return type—the data type of the value returned by the method, or void if
the method does not return a value.
 Method body can also return values:
return expression;
Access members of a class
Class A
{
int i;
char ch;
void set()
{ i=20; }
int get()
{return i; }
}
stack Heap
i
ch
A
How to access member of class A ?
A a= new A();
a.i;
a.ch;
a.set();
Types of Methods (4 basic types )
– Modifier (sometimes called a mutator)
 Changes the value associated with an attribute of the object
 E.g. A method like set()
– Accessor
 Returns the value associated with an attribute of the object
 E.g. A method like Get()
– Constructor
 Called once when the object is created (before any other
method will be invoked)
 E.g. A(int i)
– Destructor
 Called when the object is destroyed
 E.g.~A( )
Constructor
 Same name as class name
 No return type (as methods)
Why we need constructors?
 Initialize an object
Default cons (if we not defined)
– No parameter
– Ex: A()
{
}
Parameterized constructor
A(int in) A(int in, char c)
{ {
i=in; i=in;
} ch=c;
}
 Created when object init
 Can define any number of constructors
Example 2: two classes
class aa2
{
int i;
char ch;
void set()
{ i=20;}
int get()
{return i;}
}
8
public class aa4
{
public static void main(String args[])
{
aa2 obj= new aa2();
int b;
obj.set();
b= obj.get();
System.out.println("i="+ obj.i);
System.out.println("i="+ b);
}
}
Example 3: two classes uses cons.
class aa2
{
int i;
char ch;
void set()
{ i=20;}
int get()
{return i;}
aa2 (int in, char c)
{
i=in; ch=c;
}
}9
public class aa4
{
public static void main(String args[])
{
aa2 obj= new aa2(20,‘g’);
System.out.println("i="+ obj.i);
System.out.println("i="+ obj.ch);
}
}
Example 4: single class
public class aa1
{
int i;
char ch;
void set()
{ i=20;}
int get()
{return i;}
10
public static void main(String args[])
{
aa1 a= new aa1();
int b;
a.set();
b=a.get();
System.out.println("i="+ a.i);
System.out.println("i="+ b);
}
}
Introduction to Applets
 Java applet is a small appln. written in Java
 delivered to users in the form of bytecode
 user can launches Java applet from a web page
 it can appear in a frame of the web page, in a
new application window, or in Sun's
AppletViewer, a stand-alone tool for testing
applets
11
Applet Example 1
/*
<APPLET CODE="app1.class" WIDTH=150 HEIGHT=100>
</APPLET>
*/
import java.applet.Applet;
import java.awt.Graphics;
public class app1 extends Applet {
public void paint (Graphics g) {
g.drawString("Hello!",50,20);
} }
12
Applet program execution
Compile
javac app1.java
Execution
appletviewer app1.java
13
Execution through HTML file
<HTML>
<HEAD>
<TITLE> A simple Program</TITLE>
</HEAD>
<BODY> Here is the output:
<APPLET CODE="app1.class" WIDTH=150 HEIGHT=100>
</APPLET>
<BODY>
</HTML>
Store with name app1.htm
Execute from browser: C:javaapp1.htm14

More Related Content

What's hot

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
Yuriy Seniuk
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
Jussi Pohjolainen
 

What's hot (20)

Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Arrays
ArraysArrays
Arrays
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Linq inside out
Linq inside outLinq inside out
Linq inside out
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Xii Compsc Hw
Xii Compsc HwXii Compsc Hw
Xii Compsc Hw
 
Priority queues
Priority queuesPriority queues
Priority queues
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
Templates
TemplatesTemplates
Templates
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 

Similar to Lec 6 14_aug [compatibility mode]

Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]
Palak Sanghani
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
sotlsoc
 
08 class and object
08   class and object08   class and object
08 class and object
dhrubo kayal
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 

Similar to Lec 6 14_aug [compatibility mode] (20)

Java generics
Java genericsJava generics
Java generics
 
Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
08 class and object
08   class and object08   class and object
08 class and object
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Java Generics
Java GenericsJava Generics
Java Generics
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
C#
C#C#
C#
 
Java class
Java classJava class
Java class
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
 

More from Palak Sanghani

More from Palak Sanghani (20)

Survey form
Survey formSurvey form
Survey form
 
Survey
SurveySurvey
Survey
 
Nature2
Nature2Nature2
Nature2
 
Texture
TextureTexture
Texture
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
 
Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]Lec 10 10_sept [compatibility mode]
Lec 10 10_sept [compatibility mode]
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]
 
Lec 3 01_aug13
Lec 3 01_aug13Lec 3 01_aug13
Lec 3 01_aug13
 
Lec 2 30_jul13
Lec 2 30_jul13Lec 2 30_jul13
Lec 2 30_jul13
 
Lec 1 25_jul13
Lec 1 25_jul13Lec 1 25_jul13
Lec 1 25_jul13
 
Nature
NatureNature
Nature
 
Comparisionof trees
Comparisionof treesComparisionof trees
Comparisionof trees
 
My Structure Patterns
My Structure PatternsMy Structure Patterns
My Structure Patterns
 
My Similarity Patterns
My Similarity PatternsMy Similarity Patterns
My Similarity Patterns
 
My Radiation Patterns
My Radiation PatternsMy Radiation Patterns
My Radiation Patterns
 
My Gradation Patterns
My Gradation PatternsMy Gradation Patterns
My Gradation Patterns
 
My Circular Patterns
My Circular PatternsMy Circular Patterns
My Circular Patterns
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Lec 6 14_aug [compatibility mode]

  • 2. Class definition class A { int i; char ch; void set() {…} int get(int b) {…} }
  • 3. Method Declarations  General format of method declaration: Modifier return-type method-name( parameter1, …, parameterN ) { body (declarations and statements); }  Modifiers—such as public, private, and others you will learn later.  return type—the data type of the value returned by the method, or void if the method does not return a value.  Method body can also return values: return expression;
  • 4. Access members of a class Class A { int i; char ch; void set() { i=20; } int get() {return i; } } stack Heap i ch A How to access member of class A ? A a= new A(); a.i; a.ch; a.set();
  • 5. Types of Methods (4 basic types ) – Modifier (sometimes called a mutator)  Changes the value associated with an attribute of the object  E.g. A method like set() – Accessor  Returns the value associated with an attribute of the object  E.g. A method like Get() – Constructor  Called once when the object is created (before any other method will be invoked)  E.g. A(int i) – Destructor  Called when the object is destroyed  E.g.~A( )
  • 6. Constructor  Same name as class name  No return type (as methods) Why we need constructors?  Initialize an object Default cons (if we not defined) – No parameter – Ex: A() { }
  • 7. Parameterized constructor A(int in) A(int in, char c) { { i=in; i=in; } ch=c; }  Created when object init  Can define any number of constructors
  • 8. Example 2: two classes class aa2 { int i; char ch; void set() { i=20;} int get() {return i;} } 8 public class aa4 { public static void main(String args[]) { aa2 obj= new aa2(); int b; obj.set(); b= obj.get(); System.out.println("i="+ obj.i); System.out.println("i="+ b); } }
  • 9. Example 3: two classes uses cons. class aa2 { int i; char ch; void set() { i=20;} int get() {return i;} aa2 (int in, char c) { i=in; ch=c; } }9 public class aa4 { public static void main(String args[]) { aa2 obj= new aa2(20,‘g’); System.out.println("i="+ obj.i); System.out.println("i="+ obj.ch); } }
  • 10. Example 4: single class public class aa1 { int i; char ch; void set() { i=20;} int get() {return i;} 10 public static void main(String args[]) { aa1 a= new aa1(); int b; a.set(); b=a.get(); System.out.println("i="+ a.i); System.out.println("i="+ b); } }
  • 11. Introduction to Applets  Java applet is a small appln. written in Java  delivered to users in the form of bytecode  user can launches Java applet from a web page  it can appear in a frame of the web page, in a new application window, or in Sun's AppletViewer, a stand-alone tool for testing applets 11
  • 12. Applet Example 1 /* <APPLET CODE="app1.class" WIDTH=150 HEIGHT=100> </APPLET> */ import java.applet.Applet; import java.awt.Graphics; public class app1 extends Applet { public void paint (Graphics g) { g.drawString("Hello!",50,20); } } 12
  • 13. Applet program execution Compile javac app1.java Execution appletviewer app1.java 13
  • 14. Execution through HTML file <HTML> <HEAD> <TITLE> A simple Program</TITLE> </HEAD> <BODY> Here is the output: <APPLET CODE="app1.class" WIDTH=150 HEIGHT=100> </APPLET> <BODY> </HTML> Store with name app1.htm Execute from browser: C:javaapp1.htm14