Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even.

Similar presentations


Presentation on theme: "1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even."— Presentation transcript:

1 1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even void) Called automatically when an Object is created Can be overlaoded it is not inherited

2 2 Consructor Example public class Cube { public int length; public int breadth; public int height; public int getVolume() { return (length * breadth * height); } Public Cube() { length = 10; breadth = 10; height = 10; } Public Cube(int l, int b, int h) { length = l; breadth = b; height = h; } }//class

3 3 Creating objects of Cube ClassName objectName; objectName= new ConstructorName(parameterList ); Public class Test{ Public static void main(String args[]){ Cube c1=new Cube(); C1.breadth=25; Cube c2=new Cube(32,25,32); Cube c3=new Cube(); System.out.println(“volume of c1=“+c1.getVolume()); System.out.println(“volume of c2=“+c1.getVolume()); System.out.println(“volume of c3=“+c1.getVolume()); }

4 Constructor Example public class Cube { public int length; public int breadth; Public int height; public int getVolume() { return (length * breadth * height); } public Cube() { this(10, 10); } Cube(int l, int b) { this(l, b, 10); } Cube(int l, int b, int h) { length = l; breadth = b; height = h;} } 4

5 5 Class Modifioer modifier Description publicis accessible anywhere and by anyone protectedis accessible within the defining package and its subclasses blank(omitted)is accessible within the package privatemember is only accessible to the class that defines it staticused to declare it as class field abstract cannot be instantiated, must be a superclass, used whenever one or more methods are abstract final can’t be subclassed

6 6 Field Modifier ModifierDescription public is accessible by anyone, anywhere protected is accessible by methods of that class or its subclasses or by methods of classes which are in the package of class blankis accessible by methods within the same package only private is accessible by methods within the same class only staticthere is only one per class but still there could be more than one per JVM transientfield should not be serialized

7 7 Method Modifier ModifierDescription public is accessible by anyone, anywhere protected is accessible by methods within the same class or its subclasses blank is accessible by methods within the same package only private is accessible by methods within the same class only staticcannot be instantiated, are called by classname.method, can only access static variables abstractMethod is defined but contains no implementation code (implementation code is included in the subclass). If a method is abstract then the entire class must be abstract. synchronizedacquire a lock on the class for static methods acquires a lock on the instance for non-static classes

8 Abstract Class public abstract class Shape { public abstract double area(); public abstract double circumference(); } We can’t create obeject from this class 8

9 Final fields public final class Math { //… public static final double E = ; public static final double PI = ; //… } 9


Download ppt "1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even."

Similar presentations


Ads by Google