1 / 10

Review

Review. View classes as modules Encapsulate operations Functions are static methods View classes as struct types Encapsulate data View classes as abstract data types Encapsulate both data and operations. Life Cycle of Objects. Creating objects Using objects

satin
Download Presentation

Review

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. Review • View classes as modules • Encapsulate operations • Functions are static methods • View classes as struct types • Encapsulate data • View classes as abstract data types • Encapsulate both data and operations

  2. Life Cycle of Objects • Creating objects • Using objects • Cleaning up unused objects • An object becomes garbage when no variable refers to it Point p = new Point(0,0); Stack s = new Stack(); p.x = 100; rx = s.pop();

  3. Class Declaration in Java [public] [abstract] [final] class ClassName [extends ClassName] [implements Interfaces] { [VariableDeclaration | ConstructorDeclaration | MethodDeclaration | ClassDeclaration | InterfaceDeclaration]* }

  4. Declaring Variables • Access Specifier • public: accessible from anywhere • private: within the class • (default): within the class and the package • protected: within the package and subclasses • static : class variable • final: constant • Examples [AccessSpecifier] [static] [final] type VariableName Programming in Java

  5. Method Declarations MethodDeclaration ::= MethodHeadMethodBody MethodHead ::= [AccessSpecifier ] [static] [final] ReturnType MethodName(T1 p1, ..., Tn pn) MethodBody ::= { [LocalVariableDeclarationStatement | ClassOrInterfaceDeclaration | [Identifier :] Statement ]* } signature Programming in Java

  6. Method Overloading • A class can contain multiple methods that have the same name but different signatures class C { int m(int i){ return i+1; } double m(double f){ return f+2; } }

  7. Constructor • Constructor • Has the same name as the class • A class can have any number of constructors • Has no return value • Uses "this" to call other constructors in the class Programming in Java

  8. Scope of Method Arguments class Circle { int x, y, radius; public Circle(int x, int y, int radius) { this.x = x; this.y = y; this.radius = radius; } } Programming in Java

  9. The Meaning of "static" • A static variable or method belongs to the class • A static variable is shared by all objects of the class • class C { • public static void main(String[] args){ • int a = 10; • int b = 20; • System.out.println(max(a,b)); • } • int max(int x, int y){ • return (x>y) ? x : y; • } • } What is wrong?

  10. Static Variables (exercise) class AnIntegerNamedX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } } AnIntegerNamedX myX = new AnIntegerNamedX(); AnIntegerNamedX anotherX = new AnIntegerNamedX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x()); Programming in Java

More Related