SlideShare a Scribd company logo
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

PPT
3 functions and class
PPT
C++ Returning Objects
PPTX
Array within a class
PPTX
constructors and destructors in c++
PPT
Templates
PDF
04. constructor & destructor
PDF
PPTX
Templates in C++
3 functions and class
C++ Returning Objects
Array within a class
constructors and destructors in c++
Templates
04. constructor & destructor
Templates in C++

What's hot (20)

PPTX
Templates presentation
DOCX
Data Structure Project File
PPT
DOCX
C++ Template
PPTX
Linq inside out
PPT
Array Presentation (EngineerBaBu.com)
DOC
Xii Compsc Hw
PPT
Priority queues
PPT
basic concepts
PDF
Constructor and Destructor
PDF
Scala categorytheory
PPTX
C++ Constructor destructor
PPTX
Template C++ OOP
PPTX
Lambda expressions
PPT
C++: inheritance, composition, polymorphism
PPTX
Templates
PPTX
Types of Constructor in C++
ODP
Clojure basics
PPTX
Templates in c++
PDF
Data structure lab manual
Templates presentation
Data Structure Project File
C++ Template
Linq inside out
Array Presentation (EngineerBaBu.com)
Xii Compsc Hw
Priority queues
basic concepts
Constructor and Destructor
Scala categorytheory
C++ Constructor destructor
Template C++ OOP
Lambda expressions
C++: inheritance, composition, polymorphism
Templates
Types of Constructor in C++
Clojure basics
Templates in c++
Data structure lab manual
Ad

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

PDF
Lec 5 13_aug [compatibility mode]
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPTX
Lecture 5
PPTX
Chap-2 Classes & Methods.pptx
PDF
Java Programming - 04 object oriented in java
PPTX
Getters_And_Setters.pptx
PDF
02-advance-methods-on-class jdpgs code .pdf
PPTX
Hemajava
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
Classes, objects in JAVA
PPT
Ap Power Point Chpt4
PPTX
COMP1409-lesson02.pptx
PPTX
Mpl 9 oop
PPTX
Nitish Chaulagai Java1.pptx
PDF
CoreJava_training_material for beginners
PDF
Java defining classes
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
PDF
Core Java Introduction | Basics
PPT
Md03 - part3
PPTX
JAVA Module 2jdhgsjdsjdjsddsjjssdjdsjhsdjh.pptx
Lec 5 13_aug [compatibility mode]
Unit 1 Part - 3 constructor Overloading Static.ppt
Lecture 5
Chap-2 Classes & Methods.pptx
Java Programming - 04 object oriented in java
Getters_And_Setters.pptx
02-advance-methods-on-class jdpgs code .pdf
Hemajava
UNIT 3- Java- Inheritance, Multithreading.pptx
Classes, objects in JAVA
Ap Power Point Chpt4
COMP1409-lesson02.pptx
Mpl 9 oop
Nitish Chaulagai Java1.pptx
CoreJava_training_material for beginners
Java defining classes
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Core Java Introduction | Basics
Md03 - part3
JAVA Module 2jdhgsjdsjdjsddsjjssdjdsjhsdjh.pptx
Ad

More from Palak Sanghani (20)

PDF
Survey form
PDF
PPTX
PDF
PDF
Lec 11 12_sept [compatibility mode]
PDF
Lec 9 05_sept [compatibility mode]
PDF
Lec 8 03_sept [compatibility mode]
PDF
Lec 7 28_aug [compatibility mode]
PDF
Lec 10 10_sept [compatibility mode]
PDF
Lec 4 06_aug [compatibility mode]
PDF
Lec 3 01_aug13
PDF
Lec 2 30_jul13
PDF
Lec 1 25_jul13
PDF
PDF
Comparisionof trees
PDF
My Structure Patterns
PDF
My Similarity Patterns
PDF
My Radiation Patterns
PDF
My Gradation Patterns
PDF
My Circular Patterns
Survey form
Lec 11 12_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
Lec 7 28_aug [compatibility mode]
Lec 10 10_sept [compatibility mode]
Lec 4 06_aug [compatibility mode]
Lec 3 01_aug13
Lec 2 30_jul13
Lec 1 25_jul13
Comparisionof trees
My Structure Patterns
My Similarity Patterns
My Radiation Patterns
My Gradation Patterns
My Circular Patterns

Recently uploaded (20)

PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Approach and Philosophy of On baking technology
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
A Presentation on Touch Screen Technology
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
OMC Textile Division Presentation 2021.pptx
A comparative analysis of optical character recognition models for extracting...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A comparative study of natural language inference in Swahili using monolingua...
Zenith AI: Advanced Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Assigned Numbers - 2025 - Bluetooth® Document
Web App vs Mobile App What Should You Build First.pdf
Heart disease approach using modified random forest and particle swarm optimi...
A novel scalable deep ensemble learning framework for big data classification...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Approach and Philosophy of On baking technology
DP Operators-handbook-extract for the Mautical Institute
1 - Historical Antecedents, Social Consideration.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
A Presentation on Touch Screen Technology
WOOl fibre morphology and structure.pdf for textiles
OMC Textile Division Presentation 2021.pptx

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