html5-img
1 / 25

Topics To Cover

Topics To Cover. How to define a Class? Visibility of Fields Object Instantiation /Creation Accessing class Members Role of Constructor Types of Constructors. DEFINING A CLASS. SYNTAX. <scope> [<final>/<abstract>] [static>] class <classname>

zofia
Download Presentation

Topics To Cover

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. Topics To Cover • How to define a Class? • Visibility of Fields • Object Instantiation /Creation • Accessing class Members • Role of Constructor • Types of Constructors

  2. DEFINING A CLASS SYNTAX <scope> [<final>/<abstract>] [static>] class <classname> [extends <classname>] [implements <interfacename 1> …….<interface n>] { Member Variable declarations /Instance field declarations; Member Method Definitions; } Class Body <scope> : 1. package private or public for outer classes 2. private , public , protected, package private for inner classes <final> : class definition is final and can not be extended by sub classes. final class can not have sub classes <static> : static keyword can only be applied for inner classes. Outer classes can not be static. <abstract> : abstract keyword specifies that class is abstract.

  3. Instance Variable Declaration Syntax <scope> [<final>] [<static>] <type> variable name = [value]; where <scope> can beprivate, public, protected or by default friendly <type > can be any primitive type or class type or interface type; Method Declaration Syntax <scope> [<final>][<static> ][<synchronized>] [abstract] <return type> methodname( argument list ) [throws exceptiontype1..exceptiontype n] { ….. Method Body…… } Where <scope> can beprivate, public, protected or by default friendly <return type > can be any primitive type or class type or interface type; Note : Default Access is always friendly. i.e. inside the package

  4. Visibility of fields

  5. Class Examples • 1. • class xyz • { …….. } // class is defined with friendly access • 2. • public class abc • { ……...} // class is defined with public access. • Note : • public classes should be written in the same file as name of class. • public class abc should be written in file named abc.java. • In a single source file only one class can be named with public access. • 3. final class xyz • { …………} • final class means you can not create sub classes of the final class. • class abc extends xyz • { ……….} is wrong.

  6. Class Examples • 4. • abstract class xyz • { …….. } // class is defined with friendly access • abstract classes needs to be subclassed. • You can not create instances of abstract classes. • xyz x1 = new xyz(); -------- Wrong Statement. • abstract class abc • { • int a =10; • float b = 20; • abstract void show() ; // abstract method • }

  7. Exercise 1 Define a class which encapsulates a point in 2 – dimensional space. It has following members X & Y coordinate values. It supports following operations • Individual operation for setting the values for X and Y coordinates • Computing the distance between two points • Checking two points for equality [ Two points are equal if they have same values for X and Y coordinates] • Method for translating the values for X and Y

  8. Point x: double y: double +getX() : double +getY() : double +setX(x: double) : void +setY(y: double) : void +equals(other : Point) : boolean +computeDistance(other : Point) : double +show() : void UML Representation for Class Point class Point { double x; // x – coordinate double y; // y –coordinate public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public boolean equals(Point other) { return this.x == other. x && this.y == other.y ; } public double computeDistance(Point other) { double a = (this.y – other.y) * (this.y – other.y); double b = this.x – other.x) * (this.x – other.x); return Math.sqrt(a+b); } Attributes Operations public void show() { S.O.P(“ x= “+x); S.O.P(“ y= “+y); } } End of Point Class

  9. Class PointTest class PointTest { public static void main(String args[ ]) { Point P1 = new Point(); P1.show(); Point P2 = P1; P2.show(); System.out.println(P1.equals(P2)); System.out.println(P1.computeDistance(P2)); }// End of main() Method }// End of PointTest

  10. How to Create an Instance of a class (Creating Objects) Objects are always created/constructed/instantiated dynamically using new opeartor. Syntax : <ClassName> <object reference> = new ConstructorMethod(<parameters>); Constructor Method Method having same name as name of class Examples : BOX b1 = new BOX(); // Valid iff constructor is unparametrized BOX b2 = new BOX(10,6,8); // Valid iff constructor is parametrized

  11. BOX l b h b1 Object Creation Examples BOX b1 = new BOX(); BOX b2 = b1; b2 b2 is just another reference for the same object ONLY ONE OBJECT IS CREATED IN ABOVE STATEMENTS

  12. How to Access Class members • Private members of a class are only visible inside class body. • Protected members have package scope and are accessible to subclasses in other packages as well. • private protected members are only accessible to subclasses in same package or other package. [Does not have Package Scope] • public fields/methods are accessible from every where. • Every access to public or friendly access fields of class is only through objects of that class. (Except static fields which are accessible through class name) Syntax. <object_reference>.<member_field> ; <object_reference>.<methodname(parameter_list)>;

  13. Example class RectangleTest { public static void main(String args[]) { Rectangle r1 = new Rectangle(); r1.setData(10,8); r1.printSides(); System.out.println("Area ="+r1.area()); Rectangle r2 = new Rectangle(); r1.setData(100,80); r1.printSides(); System.out.println("Area ="+r2.area()); } } // End of class class Rectangle { int length; int width; void setData(int l,int w) { length = l; width = w; } void printSides() { System.out.println("Length is:"+length); System.out.println("Width is:"+width); } int area() { return length * width ;} } // End of class /* Output E:\New Folder\Java>java RectangleTest Length is:10 Width is:8 Area =80 Length is:100 Width is:80 Area =0 */

  14. class RectangleTest { public static void main(String args[]) { Rectangle r1 = new Rectangle(); r1.printSides(); r1.setData(10,8); System.out.println("Area ="+r1.area()); Rectangle r2 = new Rectangle(); r2.printSides(); r1.setData(100,80); System.out.println("Area ="+r2.area()); } } class Rectangle { int length; int width; void setData(int l,int w) { length = l; width = w; } void printSides() { System.out.println("Length is:"+length); System.out.println("Width is:"+width); } int area() { return length * width ;} } /* E:\New Folder\Java>java RectangleTest Length is:0 Width is:0 Area =80 Length is:0 Width is:0 Area =0 */

  15. Constructors • If a class has any method with the same name as its class then it is constructor method for that class • Used to initialize the objects upon creation • In the absence of constructor method we have to specifically add a method for object initialization • If no constructor is defined for the class then a default constructor is provided by Java run time environment (Without Parameters). • Constructor method has no return type not even void. • Code written inside constructor method is automatically executed for every object creation of that class.

  16. Types of Constructors • Unparametrized Constructor • Parametrized Constructor • Overloaded Constructor

  17. Unparametrized Constructor • If a class does not supply any constructor then JRE supplies a default constructor with no parameters • Class can have its own constructor of any types. • If a class supplies its own constructor then default constructor becomes hidden.

  18. Class With No Constructor class XYZ { double a,b; void setData(double x, double y) { a = x; b = y; } void print() { System.out.println("a="+a); System.out.println("b="+b); } } class XYZTEST { public static void main(String args[]) { XYZ xyz = new XYZ(); xyz.print(); } } D:\Java1>java XYZTEST a=0.0 b=0.0

  19. Class With Constructor class XYZ { double a,b; XYZ() { S.O.P(“Object XYZ created”); } } class XYZTEST { public static void main(String args[]) { XYZ xyz = new XYZ(); } } D:\Java1>java XYZTEST Object XYZ created Class with Unparametrized Constructor

  20. Class with Parametrized Constructor class XYZTEST { public static void main(String args[]) { // XYZ xyz = new XYZ(); Wrong XYZ xyz = new XYZ(10.8,6.5); xyz.print(); } } class XYZ { double a,b; XYZ(double a, double b) { this.a = a; this.b = b; print(); } void print() { System.out.println("a="+a); System.out.println("b="+b); } } D:\Java1>java XYZTEST a=10.8 b=6.5 a=10.8 b=6.5

  21. Class with Overloaded Constructor class XYZ { double a,b; XYZ() { a = 10; b = 8; print(); } XYZ(double a, double b) { this.a = a; this.b = b; print(); } void print() { System.out.println("a="+a); System.out.println("b="+b); } } class XYZTEST { public static void main(String args[]) { XYZ x1 = new XYZ(); XYZ x2 = new XYZ(10.8,6.5); x1.print(); x2.print(); } } D:\Java1>java XYZTEST a=10.0 b=8.0 a=10.8 b=6.5 a=10.0 b=8.0 a=10.8 b=6.5

  22. Constructor Examples(Parametrized Constructor) • class Triangle • { • double side1,side2,side3; • Triangle(double side1,double side2,double side3) • { • this.side1 = side1; • this.side2 = side2; • this.side3 = side3; • System.out.println ("Triangle Created with sides :"+side1 +" " +side2+ " "+side3); • } • } class TriangleTest { public static void main(String args[]) { // Triangle t1 = new Triangle(); This Line will give compile time error Triangle t1 = new Triangle(10.56,4.56,3.45); Triangle t2 = new Triangle(10,4,9); Triangle t3 = new Triangle(1,4,3); } }

  23. /* Out Put E:\New Folder\Java>javac TriangleTest.java E:\New Folder\Java>java TriangleTest Triangle Created with sides :10.56 4.56 3.45 Triangle Created with sides :10.0 4.0 9.0 Triangle Created with sides :1.0 4.0 3.0 */

  24. Overloaded Constructors • class Triangle • { • double side1,side2,side3; • Triangle(double side) • { • side1= side2 = side3 = side; • System.out.println("Equilateral Triangle Created with sides :"+side); • } • Triangle(double side1,double side2) • { • this.side1 = side1; this.side2 = this.side3 = side2; • System.out.println("Isoceles Triangle Created with sides :"+side1 +" " +side2); • } • Triangle(double side1,double side2,double side3) • { • this.side1 = side1; this.side2 = side2; this.side3 = side3; • System.out.println("Triangle Created with sides :"+side1 +" " +side2+ " "+side3); • } • }

  25. class TriangleTest { public static void main(String args[]) { Triangle t1 = new Triangle(10.56,4.56,3.45); Triangle t2 = new Triangle(10,4); Triangle t3 = new Triangle(13); } } /* Output E:\New Folder\Java>java TriangleTest Triangle Created with sides :10.56 4.56 3.45 Isoceles Triangle Created with sides :10.0 4.0 Equilateral Triangle Created with sides :13.0 */

More Related