Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.

Similar presentations


Presentation on theme: "Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University."— Presentation transcript:

1 Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.

2 Box(double w, double h, double d){ this.width=w; this.height=h; this.depth=d; } //refers the current object with this, however it is optional……………………….. Box(double width, double height, double depth){ this.width=width; this.height=height; this.depth=depth; } //refers the passed objects with same name and assigns values to class objects…… Box(double w, double h, double d){ this.width=w; this.height=h; this.depth=d; } //refers the current object with this, however it is optional……………………….. Box(double width, double height, double depth){ this.width=width; this.height=height; this.depth=depth; } //refers the passed objects with same name and assigns values to class objects……

3 //you can invoke the overridden method through the use of the keyword super. public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } //Here is a subclass, called Subclass, that overrides printMethod(): public class Subclass extends Superclass { public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } } //Compiling and executing Subclass prints the following: //Printed in Superclass. //Printed in Subclass //you can invoke the overridden method through the use of the keyword super. public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } //Here is a subclass, called Subclass, that overrides printMethod(): public class Subclass extends Superclass { public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } } //Compiling and executing Subclass prints the following: //Printed in Superclass. //Printed in Subclass

4 class room{ double width; double length; //constructor…………………………………………………………… room(double x, double y){ width=x; length=y; } //overloading constructor…………………………………………………………… room(double x){ width=length=x;} //Method with return type………………………………………. double area(){ return(length*width);} }

5 class cstdemo2{ public static void main(String args[]){ room r1= new room(10.56,2.0); room r2= new room(10.06); System.out.println("Area of the first room is " +r1.area()); System.out.println("Area of the second room is " +r2.area()); }

6 //consider class of room as discussed in previous slides open the same project but write in a new file. class bedroom extends room {double height; bedroom(double x, double y, double z){ super(x,y); height=z; } double volume(){ return(length*width*height);} }

7 class stdemo5{ public static void main(String args[]){ bedroom b1= new bedroom(14.5,12.0,8.0); System.out.println("Area of the bedroom is " +b1.area()); System.out.println("Volume of the bedroom is " +b1.volume()); }

8 //demo of static method……………………….. class mathop{ static int mul(int x, int y) {return (x*y);} static int div(int x, int y) {return (x/y);} } class stdemo3{ public static void main(String args[]){ //uses cmd line arguments…………………………………… int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); System.out.println("The multiplication is " + mathop.mul(a,b)); System.out.println("The intger division is " + mathop.div(a,b)); } } // use right click on project, select properties and Run, where you can write attributes to be passed. T hey can call only static methods. They can access only static data. They can not refer to this and super. T hey can call only static methods. They can access only static data. They can not refer to this and super.

9 To define a structure of abstraction without providing complete information of the methods inside. This is a super class with only generalization. Such a class defines nature of methods that the subclass must implement. This can be utilized in creating our own class libraries as methods may have no meaningful definitions. Some methods are required to be overridden by the subclass  use abstract type modifier.

10 final int SIZE=10; Initialization is must. The value given becomes constant. It is a tradition to write final variables in CAPITAL. By default other methods and variables can be overridden, but not final. Final variable behave like class variable and does not occupy space on object.

11 final class A {….} Extension is not possible. That is subclasses can not be extended. Final class A{ ……….} Class B extends A{ ……….} gives error. Final class A{ ……….} Class B extends A{ ……….} gives error.

12 class finalDemo{ public static void main(String args[]){ System.out.println("Final demo...."); } } class bird{ final void fly(){ System.out.println("This will not change...");} } class nonflybird extends bird{ void fly(){ System.out.prinltn("ERROR....");} }

13 Java does garbage collection automatically. When no reference of an object exists, it is no longer needed, and memory occupied by it can be reclaimed. No explicit need of manual garbage collection. The garbage collection done periodically by java run time system.

14 Java, while run time clears memory and does automatic garbage collection. But non-object references are still there in memory(eg. Window systems fonts), to clear this, the finalize() methods is used. That is when object automatically calls the garbage collector, you may choose to do some actions. Finalize() { }; Can be included in any method. Java calls this method when an object is going to be reclaimed/recycled.

15 Any variable or method defined as public is visible to the entire class. That main() is called by code that is outside the program  Java runtime system. It is also visible to all the classes outside the class in all the packages When no access specifier is used then by default the member of class is public within its own package, but not be accessed outside of its package.

16 It is by default It is visible to the classes in which it is defined

17 It is visible to the class and its all subclasses We can not override a non-private method in a subclass and then make it private. Private and Protected together  visible in all subclasses only(not in non-class) regardless of any package.

18 PublicPrivate DefaultProtected

19

20 Public grants access to anyone. Private denies access to everyone except code within that same class. Protected provides access to all code in the same packages and to subclasses in different packages. The default access restricts access to within the same package.

21

22 Main Reference: For template…. For content…. The complete reference java, Herbert schidt, Tata Macgr Visit pritisajja.info to download this show….


Download ppt "Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University."

Similar presentations


Ads by Google