1 / 38

J A V A

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.

lolita
Télécharger la présentation

J A V A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. J A V A SHASHI BHUSHAN

  2. MAIN ISSUES • Object Oriented Concepts • Examples • FAQs

  3. 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.

  4. Abstract Classes & Methods • Used to create a template class or methods. • Similar to function prototypes in C or C++.

  5. 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.

  6. 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.

  7. Abstract Classes & Methods: class Rectangle extends GraphicObject { void draw ( ) { : } } Contd.

  8. 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. …

  9. 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.

  10. 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.

  11. OOP : Interface Declaration • [Public] interface InterfaceName [extends list of SuperInterfaces]{ the interface body ------- }

  12. OOP : The Interface Body interface Collection { int MAXIMUM = 500; void add(Object obj); void delete(Object obj); Object find(Object obj); int currentCount( ); }

  13. OOP : Implementing an Interface class FIFOQueue implements Collection { ……… void add(Object obj) { ……… } Contd.

  14. OOP : Implementing an Interface void delete(Object obj) { ……… } Contd.

  15. OOP : Implementing an Interface Object find (Object obj) { ……… } Contd.

  16. OOP : Implementing an Interface int currentCount( ) { ……… } }

  17. 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.

  18. Example 1 (The Math Class) rand = Math.random( ); x = Math.abs (-123); y = Math.round (123.567); z = Math.pow (2, 4); Contd.

  19. 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.

  20. Example 1 (The Math Class) This program produces the following output: $ java MyMath 0.995526 123 124 16 le+10

  21. 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.

  22. 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.

  23. 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.

  24. 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

  25. 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.

  26. Example 3 (Using Command-Line Parameters) • This program will display the following output: • 2 + 3 = 5 • Contd.

  27. 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.

  28. 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.

  29. 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.

  30. Example 3 (Using Command-Line Parameters) • System.out.println(args[1] + args[0] + args[2]+”=“ +result); • } • }

  31. 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)); }

  32. 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); } }

  33. 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)

  34. 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.

  35. F A Q s • Where can I learn more about java and find applets and applications to play? Contd.

  36. 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.

  37. 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.

  38. F A Q s 2. What are the differences between objects and primitive data types such as int and booleans ?

More Related