Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Classes and Objects tutorials

Classes in Java

  • First of all, Java is pure object oriented programming language that is why every program in Java have at last one class.

  • A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.

  • Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

  • Methods are nothing but members of a class that provide a service for an object or perform some business logic.

  • Java fields and member functions names are case sensitive.

  • Current states of a classís corresponding object are stored in the objectís instance variables.

  • Methods define the operations that can be performed in java programming.

 

Syntax of Class

<modifier> class <class name>

{

<field declaration>;

<method declaration>

{

//method body;

}

}

 

Example of Class

public class rectangle

{

int height=10;

int width=8;

public int getarea()

{

return (height * width);

}

}


Objects in Java

  • An object is an instance of a class created using a new operator.

  • The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation.

  • An object encapsulates state and behavior. An object reference provides a handle to an object that is created and stored in memory.

  • In Java, objects can only be manipulated via references, which can be stored in variables.

  • Creating variables of your class type is similar to creating variables of primitive data types, such as integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object.

  • If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance.

  • When you create a new object, you use the new operator to instantiate the object. The new operator returns the location of the object which you assign on a reference type.

 

 

Example: Java Program to demonstrate objects and classes

 Sam Madam

//Program name RectangleObjDemo.java

//Java Program to demonstrate objects and classes

public class RectangleObjDemo //Declaring class

{

int height=10;

int width=8;

public int getarea()

{

return (height * width);

}

public static void main(String[] args) {

RectangleObjDemo rec=new RectangleObjDemo(); //Creating Object of Class

System.out.println("Area of Rectangle is :"+rec.getarea());

}

 

}

Output

Area of Rectangle is :80

 

Nested and Inner Classes

  • The Java programming language allows you to define a class within another class. Such a class is called a nested class.

  • Nested classes are divided into two categories:

    1. static.

    2. non-static.

  1. Nested classes that are declared static are simply called static nested classes.

    • A static Java nested class cannot have instances.

    • As with class methods and variables, a static nested class is associated with its outer class and like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class it can use them only through an object reference.

  1. Non-static nested classes are called inner classes.

    • A Java inner class can have instances that belong to the outer class.

    • As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.

    • Also, because an inner class is associated with an instance, it cannot define any static members itself.

Advantages of using Nested Classes

  • Nested classes can lead to more readable and maintainable code because it logically group classes in one place.

  • Nested classes also helps in code optimization as it requires less code to write.

  • Nested classes represent a special type of relationship that it can access all the members of outer class including private members also.

 

Syntax:

class <Outerclass_name>{

...

class <Innerclass_name> {

...

}

}

 

 

Example: Java program to demonstrate outer class private variables are accessed in inner nested class

 Sam Madam

//Program name Outerclass.java

// Java program to demonstrate outer class private variables are accessed in inner nested class

public class Outerclass

{

private int num = 50;

public class InnerClass

{

public void printPrivatenum()

{

System.out.println("Private number is "+num);

}

}

public void callInnerClassMethod()

{

InnerClass innerClass = new InnerClass();

innerClass.printPrivatenum();

}

public static void main(String args[])

{

Outerclass outerClass = new Outerclass();

outerClass.callInnerClassMethod();

}

}

Output

Private number is 50

 

 

Example: Java program to demonstrate static nested class

 Sam Madam

//Program name Outer.java

// Java program to demonstrate static nested class

public class Outer

{

static int i=25;

static class Inner

{

void number()

{

System.out.println("Number is "+i);

}

}

public static void main(String[] args)

{

Outer.Inner oiobj=new Outer.Inner();

oiobj.number();

}

}

Output

Number is 25

 

  • Now letís do little change in above program and make method of nested class as static

 

 

Example: Java program to demonstrate static nested class with static method

 Sam Madam

//Program name Outer.java

// Java program to demonstrate static nested class with static method

public class Outer

{

static int i=25;

static class Inner

{

static void number()

{

System.out.println("Number is "+i);

}

}

public static void main(String[] args)

{

Outer.Inner.number(); //Note you no need to create the instance of static nested class

}

}

Output

Number is 25

Pr.Pg border                                              Next Pg