1 / 75

Object Oriented Programming in Java

Object Oriented Programming in Java. Characteristics of Object Oriented Programming. Object Programming classes Encapsulation Polymorphism Inheritance Dynamic Binding. What are Objects?. Object are s/w programming models consisting of variable and related methods.

marnie
Download Presentation

Object Oriented Programming in Java

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. Object Oriented Programming in Java

  2. Characteristics of Object Oriented Programming • Object Programming classes • Encapsulation • Polymorphism • Inheritance • Dynamic Binding

  3. What are Objects? • Object are s/w programming models consisting of variable and related methods. • S/w objects are modeled after real life objects.

  4. Method 1 Variable 1 Variable 2 Method 2

  5. S/w Objects Examples: • Buttons • Scrollbar • Menu • Spreadsheet

  6. Characteristics of Real life Objects: • State • Behavior

  7. Characteristics of S/W Objects: • State (Variable) • Behavior (functions/methods)

  8. What are Classes? • A class is a s/w construct that defines the instance variables and methods of an object. • A class is itself not an object.

  9. What are Classes? A class is a blueprint / template that defines structure of similar objects.

  10. Advantages of Class • Modularity • Information hiding • Reusability

  11. Benefits of Encapsulation • Encapsulating related variables and methods into a neat s/w programming model that provides two benefits. • Modularity • Information hiding

  12. P O L Y M O R P H I S M The same message sent to the different objects result in behavior that is dependent on the nature of the object receiving the message.

  13. I N H E R I T A N C E Inheritance allows to define new classes and behavior based on existing classes to obtain code reusability and code organization.

  14. I N H E R I T A N C E Employee Part-time Employee Full-time Employee Manager Engineer Admin. staff Consultant Daily Wage Worker

  15. Advantage of Inheritance • Reduces s/w development time

  16. Dynamic Binding Objects could come from anywhere possibly across the network. You need to be able to send messages to objects without having to know their specific type at the time you write your code. It provides maximum flexibility while a program is executing.

  17. OBJECT ORIENTED PROGRAMMING IN JAVA Creating, Declaring & Instantiating an object: Date tomorrow = new Date( ); This single statement actually performs three functions: Declaration : Instantiation: Initialisation

  18. Declaration of an Object: Date tomorrow; Instantiation of an object: Contd.

  19. Declaration of an Object: The new operator instantiates a class by allocating memory for a new object of that type. The new requires a single argument: a call to a constructor method. Contd.

  20. Constructor Methods: Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type. The new operator creates the object and the constructor initializes it. Contd.

  21. Declaration of an Object: Date tomorrow = new Date( ) does not take any argument.

  22. Declaration of an Object: A constructor that takes no argument is called default constructor. Like Date ( ), most classes have at least one default constructor. Contd.

  23. Declaration of an Object: If a class has multiple constructors, they all have the same name but different number or types of arguments. Date class type provides with another constructor that initialises the new Date with a year, month,day and date. Date HisBirthday = new Date (1971, 6, 30);

  24. OOP : GARBAGE COLLECTOR • No need of keeping track of objects for destroying. • The Java runtime environment has a garbage collector that frees the memory used by objects that are no longer needed. Contd.

  25. OOP : GARBAGE COLLECTOR • An object is eligible for garbage collection when there are no more references to objects. • The garbage collector run in a low-priority thread and runs both synchronously and asynchronously depending on the situation and the system on which Java is running.

  26. OOP : CLASS DECLARATION A class declaration looks like this: [modifiers] class ClassName [extends SuperClassName] [implements Interface Names]{ -------- } The items between the square are optional. Contd.

  27. OOP : CLASS DECLARATION • modifiers declares whether the class is public, abstract or final. • extends is used to specify a single inheritance mechanism. • Implements is used to specify multiple inheritance.

  28. OOP : Declaring a class’s superclass • In Java, every class has a superclass. If you don’t specify a superclass for your class it is assumed to be the Object class (declared in Java.lang). • The keyword extends is used to specify the superclass. Contd.

  29. OOP : Declaring a class’s superclass Class NameOfClass extends SuperClassName { ---------- ---------- }

  30. OOP : Public, Abstract and Final Class • The Public modifier declares that the class can be used by objects outside the current package. Contd.

  31. OOP : Public, Abstract and Final Class • The abstract modifiers declares that the abstract class may contain methods (without any implementation). These classes are intended to be sub-classed and can not be instantiated.

  32. OOP : Public, Abstract and Final Class (Contd...) • The Final modifier declares that this class can not be sub-classed for security reasons & design reasons. • It does not make sense for a class to be both final and abstract.

  33. Package: • A package is a container of related classes and interfaces • Used to access related classes as well as to hide the internals of a package. Contd.

  34. Package: • Package is declared using package keyword • Package my-package; • Package java.awt.image; • All of Java built-in classes are put under the Java package. Six sub packages are defined under the Java package • Java.lang, Java.awt, Java.io, Java.net, Java.applet, Java.util

  35. Object Oriented Feature • The Import Statement • The Import statement loads existing classes for using their definition and methods. Contd.

  36. Object Oriented Feature Example: • Importing the class from the package: import Java.awt.Graphics; • Importing the entire package that the class belongs to: • Import Java.awt.*.

  37. OOP : Access control mechanism: • PRIVATE • PROTECTED • PUBLIC • STATIC • FINAL • ABSTRACT

  38. OOP : Access control mechanism:PRIVATE: • A PRIVATE MEMBER AND METHOD ARE ACCESSIBLE ONLY TO THE CLASS IN WHICH IT IS DEFINED. Contd. ...

  39. OOP : Access control mechanism:PRIVATE: EXAMPLE: class Alpha { private int privateX; private void privateMethod( ){ System.out.println(“PrivateMethod”); } } Contd. ...

  40. OOP : Access control mechanism: PRIVATE: Class Beta { void accessMethod ( ) { Alpha a = new Alpha ( ) ; a.privateX = 15; // illegal a.privateMethod ( ) ; // illegal } } Contd.

  41. OOP : Access control mechanism:PRIVATE: Beta class cannot access private variable and private methods on an object of type Alpha because Beta is not the type of Alpha.

  42. OOP : Access control mechanism: PRIVATE: Objects of same type can access to one another’s private members. This is because access restriction apply at the class or type level rather than at the object level. Contd.

  43. Example: class Alpha { private int privateX ; boolean isEqualTo (Alpha anotherAlpha) { if (this.privateX= =anotherAlpha.privateX) //legal return true; else return false; } } this is a java language keyword that refers to the current object.

  44. Protected • It allows the class itself, subclasses and all classes in the same package to access the members. Contd. ...

  45. Protected Package Greek ; Class Alpha { //declared in a Greek package protected int protectX ; protected void protectedMethods ( ) { system.out.println(“protectedMethods”) } }

  46. Protected • Suppose class Gama is also declared to the member of the Greek package. The class can legally access an alpha object’s privateX member variable and protected.

  47. Protected Methods Package Greek ; Class Gamma{ void access Method ( ) { Alpha a = new Alpha ( ) ; a.protectX=10; // legal a.protectedMethod ( ) ; // legal } }

  48. Public • Any class in any package has access to class’s public member. Declare members to be public only if such access cann’t produce undesirable results if an outsider uses them. Contd. ...

  49. Public Package Greek ; public class Alpha { public int publicX; public void publicMethod ( ) { System.out.println (“publicMethod”); } } Contd. ...

  50. Public • Beta class is defined in a different package and not a sub class of Alpha. Contd.

More Related