j a v a shashi bhushan. main issues object oriented concepts examples faqs
TRANSCRIPT
![Page 1: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/1.jpg)
J A V A
SHASHI BHUSHAN
![Page 2: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/2.jpg)
MAIN ISSUES
Object Oriented Concepts
Examples
FAQs
![Page 3: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/3.jpg)
Abstract Classes & Methods
An abstract class is a class that can only be sub-classed - it can not be instantiated.
It contains abstract methods with no implementation.
Contd.
![Page 4: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/4.jpg)
Abstract Classes & Methods
Used to create a template class or methods.
Similar to function prototypes in C or C++.
![Page 5: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/5.jpg)
Abstract Classes & Methods
abstract class GraphicsObject {int x , y;:void moveTo (int newX, int newY) {:}abstract void draw ( ) ; // to be implemented
by all subclasses //
}
Contd.
![Page 6: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/6.jpg)
Abstract Classes & Methods:
Each non-abstract subclass of Graphic object such as circle and rectangle has to provide an implementation for the draw method.
class Circle extends GraphicObject {void draw ( ) {:}
}
Contd.
![Page 7: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/7.jpg)
Abstract Classes & Methods:
class Rectangle extends GraphicObject {
void draw ( ) {:}}
Contd.
![Page 8: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/8.jpg)
OOP : Interface
An interface is a collection of abstract methods (without implementations) and constant values
Interfaces add most of the functionality that is required for many applications which would normally resort to using multiple inheritance in a language such as C++.
Contd. …
![Page 9: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/9.jpg)
OOP : Interface
Interfaces are useful for:
Capturing similarities between unrelated classes without forcing a class relationship.
Declaring methods that one or more classes are expected to implement.
![Page 10: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/10.jpg)
Interfaces Do Not Provide Multiple Inheritance:
The interface hierarchy is independent of the class hierarchy – classes that implement the same interface may or may not be related through the class hierarchy. This is not true for multiple inheritance.
![Page 11: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/11.jpg)
OOP : Interface Declaration
[Public] interface InterfaceName
[extends list of SuperInterfaces]{
the interface body
-------
}
![Page 12: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/12.jpg)
OOP : The Interface Bodyinterface Collection {
int MAXIMUM = 500;
void add(Object obj);
void delete(Object obj);
Object find(Object obj);
int currentCount( );
}
![Page 13: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/13.jpg)
OOP : Implementing an Interface
class FIFOQueue implements Collection {
………
void add(Object obj) {
………
}
Contd.
![Page 14: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/14.jpg)
OOP : Implementing an Interface
void delete(Object obj) {
………
}
Contd.
![Page 15: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/15.jpg)
OOP : Implementing an Interface
Object find (Object obj) {
………
}
Contd.
![Page 16: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/16.jpg)
OOP : Implementing an Interface
int currentCount( ) {
………
}
}
![Page 17: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/17.jpg)
Example 1 (The Math Class)
Below is a small program that uses some of the Math class methods:
public class MyMath {
public static void main(String args[ ] ) {
int x;
double rand, y, z;
float max;Contd.
![Page 18: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/18.jpg)
Example 1 (The Math Class)
rand = Math.random( );
x = Math.abs (-123);
y = Math.round (123.567);
z = Math.pow (2, 4);
Contd.
![Page 19: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/19.jpg)
Example 1 (The Math Class)
System.out.println(rand);System.out.println(x); System.out.println(y); System.out.println(z); System.out.println(max);
}
}
Contd.
![Page 20: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/20.jpg)
Example 1 (The Math Class)
This program produces the following output:$ java MyMath0.99552612312416le+10
![Page 21: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/21.jpg)
Example 2 (The Character Class)
This program below uses some of the methods in the Character class:public class Mychar { public static void main (String args[ ]) {
char c1 = ‘A’;char c2 = ‘z’;char c3 = ‘5’;
Contd.
![Page 22: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/22.jpg)
Example 2 (The Character Class)
System.out.println(“Is character” + c1 +” uppercase?” +
Character.isUpperCase(c1));
System.out.println(“Is character” + c2 +” uppercase?” +
Character.isUpperCase(c2));
Contd.
![Page 23: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/23.jpg)
Example 2 (The Character Class)
System.out.println(“Is character” + c3 +” a digit? ” +
Character.isDigit(c3));
System.out.println(“Convert” + c1 +” to lowerercase:” +
Character.toLowerrCase(c1));
}
}
Contd.
![Page 24: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/24.jpg)
Example 2 (The Character Class)
This program produces the following output:
$ java Mychar
Is character A uppercase? true
Is character z uppercase? false
Is character 5 a digit? true
Convert A to lowercase: a
![Page 25: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/25.jpg)
Example 3 (Using Command-Line Parameters)In this example, a program is written that will perform binary operations on integers. The program receives three parameters: an operator and two integers. For example, to add two integers, the following command could be used:
java TestCommandParameters + 2 3
Contd.
![Page 26: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/26.jpg)
Example 3 (Using Command-Line Parameters)This program will display the following output:
2 + 3 = 5
Contd.
![Page 27: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/27.jpg)
Example 3 (Using Command-Line Parameters)public class TestCommandParametrs
{ public static void main (String [ ] args)
{ int result = 0;
if (args.length ! = 3)
{ System.out.println(
“please use java operator operand1 operand2”);
System.exit(0);
} Contd.
![Page 28: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/28.jpg)
Example 3 (Using Command-Line Parameters)switch (args[0].charAt(0))
{ case ‘+’ : result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
{ case ‘ - ’ : result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;
Contd.
![Page 29: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/29.jpg)
Example 3 (Using Command-Line Parameters){ case ‘ * ’ : result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
{ case ‘ / ’ : result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
break;
Contd.
![Page 30: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/30.jpg)
Example 3 (Using Command-Line Parameters)
System.out.println(args[1] + args[0] + args[2]+”=“ +result);
}
}
![Page 31: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/31.jpg)
Example 4 (Computing Fibonacci Numbers)
public class TestFibonacci{
public static void main ( String args[ ] )
{
int n = 5;
System.out.println (“fibonacci number at index “+n” is+ fib (n));
}
![Page 32: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/32.jpg)
Example 4 (Computing Fibonacci Numbers)
public long fib (long n)
{
if (n == 0) || (n==1)
return 1
else
return fib(n-1) + fib(n-2);
}
}
![Page 33: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/33.jpg)
Recursive Callsfib(5)
fib(4) fib(3)
fib(3) fib(2)
fib(2) fib(1)
fib(2) fib(1) fib(1) fib(0)
fib(1) fib(0)
fib(1) fib(0)
![Page 34: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/34.jpg)
F A Q s
What is the relationship between java and JavaScript?What is the difference between instance variables and methods and class variables and methods?
Contd.
![Page 35: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/35.jpg)
F A Q s
Where can I learn more about java and find applets and applications to play?
Contd.
![Page 36: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/36.jpg)
F A Q s
1. http://www.java.sun.com obtaining source for java information
2. http://www.gamelan.com for applets3. Newsgroups
* Comp.lang.java* Comp.lang.java.programme* Comp.lang.java.tech
Contd.
![Page 37: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/37.jpg)
F A Q s
1.If arrays are objects and you use new to create them and they have an instance variable length, where is the array class?
Contd.
![Page 38: J A V A SHASHI BHUSHAN. MAIN ISSUES Object Oriented Concepts Examples FAQs](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649d835503460f94a68d49/html5/thumbnails/38.jpg)
F A Q s
2. What are the differences between objects and primitive data types such as int and booleans ?