180 likes | 314 Views
What’s Wrong With Following class Declarations. final abstract class A { }. final and abstract can not be used together for class definition. static class A { }. Static keyword can only be applied for Inner classes. private final class A { }.
E N D
What’s Wrong With Following class Declarations final abstract class A { } final and abstract can not be used together for class definition static class A { } Static keyword can only be applied for Inner classes private final class A { } Outer classes can have either public or package scope <package private> final class A extends B { } OK abstract class A extends B { } OK
Public class Example // abc.java public class A { } public class B { } public class C { } What’s wrong here D:\java\bin>javac abc.java abc.java:1: class A is public, should be declared in a file named A.java public class A ^ abc.java:4: class B is public, should be declared in a file named B.java public class B ^ abc.java:7: class C is public, should be declared in a file named C.java public class C ^ 3 errors
Public class Example cont….. Requirement for Successful Compilation public class A { } class B { public static void main(String args[]) { System.out.println("Hello"); } } File should be named as A.java D:\java\bin>javac A.java D:\java\bin>java B Hello
Inner class Example[Nested Classes] What’s Wrong Here class Outer { class A {} static class B{ } final static class C { } private abstract class D {} protected class E { } }// End of class Outer OK OK OK OK OK
Inner class Example[Local Classes] class Outer { public void show() { class A { }// End of class A }// End of show() Method }// End of class Outer NOTE : Local Inner class is only local to the method & hence can not be declared as public, private, protected etc.
Instance Field vs Local Variable class ABC { private int a; private double b; void sample() { int a; double b; ………………….. …………………. }// End of sample() Method }// end of class ABC Instance Fields / Attributes of class Local Variables Local Variables have only method scope. They can not be declared as public, private etc.
Instance Field vs Local Variable cont…. • Local Variables are not automatically initialized where as instance fields are automatically initialized to some default value depending upon their type. class Lecture4 { public static void main(String[] args) { int x; System.out.println("Value of X:"+x); } } D:\java\bin>javac Lecture4.java Lecture4.java:6: variable x might not have been initialized System.out.println("Value of X:"+x);
class Lecture4 { public static void main(String[] args) { Student std = new Student(); std.display(); } } class Student { String name; String idno; int age; double cgpa; float amount; boolean isMale; void display() { System.out.println("Name:"+name); System.out.println("idno:"+idno); System.out.println("age:"+age); System.out.println("AGPA:"+cgpa); System.out.println("Amount:"+amount); System.out.println("Is male:"+isMale); } } D:\java\bin>javac Lecture4.java D:\java\bin>java Lecture4 Name:null idno:null age:0 AGPA:0.0 Amount:0.0 Is male:false
Use of this Pointer • this pointer in java is used to resolve the conflict between method parameters and instance fields if they are same • Through this pointer we can refer directly to instance fields / methods. • This always points to the instance fields/methods of the invoked class • Usage this.<instance Variable Name> this.<instance_method>(<parameters>);
Example of this class Rect { double length; double width; Rect(double length,double width) { this.length = length; this.width = width; this.print(); // print(); } void print() { System.out.println("Length:"+length); System.out.println("Width:"+width); } } Rect(double l,double w) { length = l; width = w; print(); ; }
Point Class class Point { double x; double y; // Overloaded Constructors Point() { x=y=0.0; } Point(double x, double y) { this.x =x; this.y =y; } Point(double value,boolean isPointonXaxis) { if(isPointonXaxis) {x=value; y =0;} else {x=0; y = value;} } Attributes / Instance Fields Constructor for origin Constructor with values x and y Constructor for Creating a point on X-axis or Y-axis
double computeDistance(Point other) { return Math.sqrt((this.y - other.y)*(this.y - other.y)+(this.x - other.x)*(this.x - other.x)); } boolean isEqual(Point other) { if (x == other.x && y == other.y) return true; return false; } boolean isOnXaxis() { if (y == 0.0 && x != 0.0) return true; return false; } boolean isOnYaxis() { if (x == 0.0 && y != 0.0) return true; return false; } Method to compute Distance Method to for equality checking Method to check whether the point lies on X-axis Method to check whether the point lies on Y-axis
boolean isOrigin() { if (x == 0.0 && y == 0.0) return true; return false; } void print() { System.out.println("X="+x); System.out.println("Y="+y); } }// End of class Point Method to check whether the point is origin Method to print values for x and y coordinates
PointTest class class PointTest { public static void main(String args[]) { Point p1 = new Point(10,6); Point p2 = new Point(); Point p3 = new Point(); Point p4 = new Point(5,true); Point p5 = new Point(5,false); System.out.println("Distance between p1 & p2 is:"+ p1.computeDistance(p2)); System.out.println(" p1 & p2 Equals:"+ p1.isEqual(p2)); } } Creates a point with x =10 , y=6 Creates a point with x =0 , y=0 Creates a point with x =5 , y=0 Creates a point with x =0 , y=5 D:\java\bin>java PointTest Distance between p1 & p2 is:11.661903789690601 p1 & p2 Equals:false
Exercises Example 2: Defile a class box with following fields length, width, height Provide the operations to compute surface area, volume of box. Provide the operations to print the dimensions of a box and to compare two box instances for equality. Write a test class for the same.
class BOX { double l,b,h; BOX(double l,double b,double h) { this.l = l; this.b = b; this.h = h;} BOX(double l) { this.l=this.b=this.h = l;} BOX(double l, double b) { this.l = this.b = l; this. h = b;} double area() { return 2*(l*b+b*h+h*l); } double volume(){ return l*b*h ; } void printDimension() { System.out.println("Length ="+l); System.out.println("Width ="+b); System.out.println("Height ="+h); } boolean isEqual(BOX other) { if(this.volume() == other.volume()) return true; return false; } }
class boxtest { public static void main(String args[]) { BOX b1 = new BOX(20); BOX b2 = new BOX(10,40,20); BOX b3 = new BOX(20,10); b1.printDimension(); b2.printDimension(); b3.printDimension(); System.out.println("Area of b1="+b1.area()); System.out.println("Area of b2="+b2.area()); System.out.println("Area of b3="+b3.area()); System.out.println("Volume of b1="+b1.volume()); System.out.println("Volume of b2="+b2.volume()); System.out.println("Volume of b3="+b3.volume()); System.out.println(b1.isEqual(b2)); System.out.println(b2.isEqual(b3)); System.out.println(b3.isEqual(b1)); } }