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...