Posts

Method Overriding in Java

Image
Method overriding is defining method in subclass with same signature with specific implementation in respect to the subclass. Overriding means one version is parent class and one version in child class and signature is also same but both are not in same class. The method of parent class will be hidden. Method Overriding in Java: In Method Overriding this things are same: Method Name. Parameter. Return type. But use in inheritance.  Diagram: subclass implement this method by own choice. Here Short Program: class A { public void F(int x) System.out.println("Class A"); } class B extends A { public void F(int x) System.out.println("Class B"); } public class Overriding { public static void main(String[]args) { B b = new B(); b.F(5); } } Now the function of parent class is hid by child or sub class....

When Constructors are Called?

 Constructors is always called from super class to sub classes.  super() execute very first statement of constructor in a subclass  and this order is same. If super() is not used default constructor is execute of all super class. // See constructors are called. // Create super class. class X {   X() {     System.out.println("Inside X constructor.");   } } // Create a subclass by extending class X. class Y extends X {   Y() {     System.out.println("Inside Y constructor.");   } } // Create another subclass by extending Y. class Z extends Y {   Z() {     System.out.println("Inside Z constructor.");   } }   class CallingConstructor {   public static void main(String args[]) {     Z z = new Z();   } } Output: Inside X constructor. Inside Y constructor. Inside Z constructor

Member Access and Inheritance

Member Access and Inheritance: Although a subclass includes all of the members of its superclass, but cannot access those members of the superclass that  declare as private. Here the Example : // This program uses inheritance to extend Box. class Box {   double width;   double height;   double depth;   // construct clone of an object   Box(Box ob) { // pass object to constructor     width = ob.width;     height = ob.height;     depth = ob.depth;   } // constructor used when all dimensions specified   Box(double w, double h, double d) {     width = w;     height = h;     depth = d;   }   // constructor used when no dimensions specified   Box() {     width = -1;  // use -1 to indicate     height = -1; // an uninitialized     depth = -1;  // box   } // constructor used when cube is created...

bitwise operators in java with example programs

The Program is look like this: class BitLogic{  public static void main(String[] args) {             String binary[] = {       "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "10010", "10100", "10110", "11000", "11010", "11100", "11110" };     int a = 3; // 0 + 2 + 1 or 0011 in binary     int b = 6; // 4 + 2 + 0 or 0110 in binary     int c = a | b;     int d = a & b;     int e = a ^ b; int f = (~a & b) | (a & ~b); int g = ~a & 0x0f; System.out.println("        a = " + binary[a]);     System.out.println("        b = " + binary[b]);     System.out.println("      a|b = " + binary[c]);     System.out.println("      a&b = " + binary[d]);     System.out.println("      a^b = " + binary[e]);   ...

java exponent operator?

java exponent operator: Java  exponent operator means 5^n. Here a short or simple program of Java exponent operator and how its works: ___________________________________________________________________ Program: package exponent.operator; // Package import java.util.Scanner;   // Import Java Scanner Library for input public class ExponentOperator {  // Class          public static void main(String[] args) {  // Main Class                  Scanner sc = new Scanner(System.in);    // Scanner object              // Initialize variables           int  Base;               int Powernumber;           //for intput of number and power                ...

Inheritance in Java

   We can create a general class that defines traits common to a set of related items. This class can then be   inherited by others , more specific classes, each adding those things that  are unique to it A classes that is inherited are called a super classes.   The classes that does the inheritance is  called a sub classes. Therefore, a subclass is a specialized version of a superclass.    To inherit a class, we simply incorporate the definition of one class into another by using the  extends keyword.    The general form of a subclass declaration is:   class subclass-name extends super class-name   { /*body of class*/ }    We can specify only one super class for any subclass.    This is because Java does not support multiple inheritance .

Operators in Java Language?

Four Types of Operators in Java Language and these are: Arithmetic.  Bitwise. Boolean. Relation Operators.  Arithmetic Operators: Operands must be numeric type of arithmetic operators. We can not use these operators as a Boolean Type but we can us them as a char type.  These operators are:        Operators                       Results             +                                  Addition            -                                     Subtraction           *                                       Multiplicatio...

What is Variable-Length Arguments in Java Language?

Variable-Length Arguments is Java Language J2SE 7 added new Feature that simplifies creation of methods that need to take a Variable number of arguments. This Feature is known as Varargs short for Variable-Length Arguments. In some situations require that variable number of arguments passed to method are not unique. ________________________________________________________ Example: A method for opening internet connection take this information: User name. Password Filename Protocol etc etc If some information is not supplied by supply defaults. ___________________________________________________________________ A variable-length arguments is specified by three term. For example: How vaTest()//(Method) is written using vararg. v is implicitly declare as array of int type. Inside vaTest() v is accessed by using normal array syntax. ___________________________________________________________________ Decl...

What is Nested and Inner Classes in Java Language?

                                 Nested and Inner Classes It is possible in Java language to define a one class within another class these classes are called as nested classes. Scope of Nested Classes: The Scope of all Nested class is bounded by the scope of its inserting  class. Example: If  B class is Define within class A then the class B is only Known to A and not outside of class A and the scope is in class only class A. Nested class has access all members including private members. But inserting class does not have access the nested class members. Type of Nested Classes: There are two types of Nested classes: Static Nested Class. Non-Static Nested Class. 1.Static Nested Class: Static Nested class is one in which static modifier applied. Member of inserting class must access by an object. Static Nested Class can't refer to members of its inserting ...

Access Control in Java

How a member can be accessed is determine by the access specifier that modifies its declaration. Java's Access Specifiers are public private protected default If no access specifies is used then by default the member of a class is public within its own package but cannot be accessed outside of its package. Some difference Between Access Control: 1.Public: The public Access specifies are  visible in full project or code. Declaration:                                public int a; 2. Private:                  The private Access specifies are  visible only in class you can not access these specifies outside the class or method if declare in method. Declaration:                             private int a; 3.Protected:     ...

this keyword in Java

   What problems to need this keyword in Java?                    It is illegal in Java Language to declare two variables with same name inside the same scopes. When the local variables and instance variables has same name then local variables hides the instance variables. Solution of problems: The this keyword lets us refer directly  to the object we can use it to resolve any name-space collisions that occurs between local and instance variables. In Java Language Java define the  this keyword. this keyword is used to resolve the name-space collisions. The this keyword is used to inside any method to refer to the any object. Sometimes a method need to refer to the object that invoked it.  this keyword is always a reference to the object on which the method was invoked     Use this keyword to resolve name-space collisions: Box(double width, double heigh...

Tool for Java Coding

Image
Short or Simple Overview of Java Tool NetBeans IDE 8.0.2: NetBeans IDE 8.0.2 look this this: Interface of NetBeans IDE 8.0.2 look this this:   You can create your Project select file in left corner and then select New Project or in short key  Press Ctrl+Shift+N your project is create. Now interface look like this select Java. When select Java click Next button. When you click Next button interface change and look like this now you can change your project name if u want otherwise project name is that which is in arrow box. Now then click Finish button your Project is created. Your Project First time always Look like this.   In Boxes these are all Comments you can remove from if u want and Now you can code and make your Project.

Constructor

 Constructor rules: Constructor has the same name as the class in which it locate. Constructors have no return type, not even void. This is because type of a class constructor is the class type itself . Here constructor of Student class. class Student{ string name; int age; //Here Default constructor or without parameter Student(){  name="abc"; age=20; } } When we don’t clearly define a constructor for a class, then Java creates a default constructor for the class. The default constructor automatically initializes all instance variables to zero. Once we define our own constructor, the default constructor is no longer used in class. Parameterized Constructor: class Student{ string name; int age; //Here constructor Student(string n,int a){  name=n; age=a; } }

Classes in Java

Class? Class is template or blueprint. The most important thing to understand about a class is that it  defines  a new data type. Variables defined within a class are called instance variables because each instance of the class contains its copy of these variables.   Class declaration Method : class ClassName { Instance Variables:   type instanceVariable1;   type instanceVariable2;   . . .   type instanceVariableN; Methods in Class:   type methodName1(parameterList){   //body of method and code   }   type methodName2(parameterList){   //body of method   }   . . .   type methodNameN(parameterList){   //body of method and code   } } Declare Object of class: Classname objectname = new classname(); For Example Student stud = new Student();

Type Casting or Conversion in Java

In Java language type casting or conversion means convert the value of  one data type into another compatible data type variable. In simple word type casting or type conversion means convert int data type value  into long data type. Two conditions for type casting and conversion: Both types are compatible e.g int into long. Destination type is larger then source type. All data types are not compatible and all types of conversions all not allowed automatic or implicitly . For example you can not convert double into byte data type.                               But...... It is still possible in java to convert incompatible data types by using cast ., cast perform explicit conversion between two incompatible data types.   Automatic type conversion: Convert automatically two types like this: byte a=1; byte b=12; int sum=a+b;   Incompatible Types conversion by Casting...

declare variable in java

Variables are basic unit of storage. All variables have scope the scope of variable is define its visibility.Variables are define by data types and  variable name. Declaring a Variable  : In java variable can be declared before its used. Example of  Declaring a Variable: if  Declare one Variable   syntax      int a; if   Declare more then one Variable then syntax is    int a,b,c,d;  Declare Variable with value  :       int a=12; if   Declare more then one Variable then syntax is     int a=12,b=13,c=14,d=15;

Data type's in java language

Java is strongly typed language. Primitive Data Types:    Java has eight primitive Data types but can be put in four groups.  Integer Floating-point Characters Boolean Integer: Java has four integer types  byte  short  int  long       All types are signed, positive and negative values. Name Width Range byte  8bits  128 to 127  short  16bits  –32,768 to 32,76   int  32bits  –2,147,483,648 to 2,147,483,647  l ong  64bits  –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Floating-point: Java has  two floating-point types Double  float   Name  width   Approx. Range  float  32bits 1.4e–045 to 3.4e+038   double  64bits  4.9e–324 to 1.8e+308 ...

Control Statement's in Java

Control Statements means the control of program is change according to that condtions. Java supports following control statements these are: if statement:      syntax is                    if(condition)//code;                          else//code;   Nested ifs Statement:                syntax is                    if(condition){//code;                        if(condition)//code;                           if(condition)//code;                          else//code;}                         ...