380 likes | 524 Views
J A V A. SHASHI BHUSHAN . MAIN ISSUES. Object Oriented Concepts Examples FAQs. 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.
E N D
J A V A SHASHI BHUSHAN
MAIN ISSUES • Object Oriented Concepts • Examples • FAQs
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.
Abstract Classes & Methods • Used to create a template class or methods. • Similar to function prototypes in C or C++.
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.
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.
Abstract Classes & Methods: class Rectangle extends GraphicObject { void draw ( ) { : } } Contd.
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. …
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.
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.
OOP : Interface Declaration • [Public] interface InterfaceName [extends list of SuperInterfaces]{ the interface body ------- }
OOP : The Interface Body interface Collection { int MAXIMUM = 500; void add(Object obj); void delete(Object obj); Object find(Object obj); int currentCount( ); }
OOP : Implementing an Interface class FIFOQueue implements Collection { ……… void add(Object obj) { ……… } Contd.
OOP : Implementing an Interface void delete(Object obj) { ……… } Contd.
OOP : Implementing an Interface Object find (Object obj) { ……… } Contd.
OOP : Implementing an Interface int currentCount( ) { ……… } }
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.
Example 1 (The Math Class) rand = Math.random( ); x = Math.abs (-123); y = Math.round (123.567); z = Math.pow (2, 4); Contd.
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.
Example 1 (The Math Class) This program produces the following output: $ java MyMath 0.995526 123 124 16 le+10
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.
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.
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.
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
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.
Example 3 (Using Command-Line Parameters) • This program will display the following output: • 2 + 3 = 5 • Contd.
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.
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.
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.
Example 3 (Using Command-Line Parameters) • System.out.println(args[1] + args[0] + args[2]+”=“ +result); • } • }
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)); }
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); } }
Recursive Calls fib(5) fib(3) fib(4) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0)
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.
F A Q s • Where can I learn more about java and find applets and applications to play? Contd.
F A Q s • http://www.java.sun.com obtaining source for java information • http://www.gamelan.com for applets • Newsgroups * Comp.lang.java * Comp.lang.java.programme * Comp.lang.java.tech Contd.
F A Q s • If arrays are objects and you use new to create them and they have an instance variable length, where is the array class? Contd.
F A Q s 2. What are the differences between objects and primitive data types such as int and booleans ?