Tuesday, September 30, 2014

Abstract Cass

Abstract Class


Abstract classes are used to achieve abstraction. Any class which contains an abstract method should be declared as abstract. We can’t create object of an abstract class. Abstract class can also contain concrete methods so that’s why it doesn’t provide 100% abstraction.

Syntax:           abstract class class-name {  }

abstract is a keyword which is used to achieve abstraction.

Abstract class doesn’t contain the definition of abstract method. It only contains the declaration of the abstract method. Abstract methods are defined to achieve runtime polymorphism. An abstract method can’t be a private, static and final method. Abstract class can contain more than one abstract functions but subclass should contain the definition of all the abstract methods.  Example:          
                               
abstract class A
{
      abstract void absfun(); // abstract method ends with semicolon.
      public void concfun()
      {
            System.out.println("concrete method");
      }
}
class C extends A
{
      void absfun()
      {
            // definition of abstract method in derived class.
            System.out.println("abstract method");
      }
      public static void main(String args[])
      {
            C ob= new C();
            ob.absfun();
            ob.concfun();
      }
}
Output: abstract method
        Concrete method

If any class is derived from an abstract class then it is subclass’s responsibility to fulfil the body part of abstract method of superclass. And if any derived class doesn’t fulfil the body part of abstract method of super class then it is necessary to declare the derived class as abstract class. One abstract class can also inherit or extends another abstract class.  

Abstract class can also contain constructors, member variables. Abstract class is different from interface. Object of abstract class can’t be created. Abstract class must contain ab abstract method. Abstract methods are used where two or more subclasses are expected to do similar things in different ways through different implementation. Let’s see an example:  

abstract class figure
{
public int dim1=10, dim2=20;
abstract int area (); // abstract method ends with semicolon.
}
class rectangle extends figure
{
int area()
{
return(dim1*dim2);
}
}
class triangle extends figure
{
int area()
{
return((dim1*dim2)/2);
}
}
class abstractcls
{
public static void main(String args[])
{
//figure ob= new figure();  illegal, coz we can’t create instance of abstract class
figure ob= new rectangle(); // or rectangle ob= new rectangle();
triangle ob1= new triangle();
System.out.println("area of rect = "+ ob.area()+"\n area of tri = "+ ob1.area());
}
}
Output: area of rect = 200
        area of tri = 100

Abstract methods are used where two or more subclasses are expected to do similar things in different ways through different implementation. Abstraction is used to hide the complexity.


Thursday, September 25, 2014

Static Import Statement

Static Import Statement

As you know that import statement import the standard as well as user defined packages in java source file. In this topic we will discuss why import the packages statically i.e. by using static import.

So in the answer I would say, by using static import, we can access static data types and static methods directly in to class. Let’s take an example. But before that we should know the syntax.

Syntax:   import static packagename1 [ . packagename2 [ . packagenameN ]].  classname.(static field/static method/*) ;

Example:  import static java.lang.system.*;
                   import static java.lang.system.out;

java language has many standard packages. One of them is lang . lang package contain many classes like Syatem, Math. These classes contain static methods and fields which we can use directly. Let’s understand this static import statement with Math class.

Math class
Class java.lang.Math {
            public static double pow (double a , double b){
            // body part of pow method
}
public static double sqrt (double a){
            // body part of sqrt method
}
// rest of the methods
}

Math class would probably has this type of body. So if we do not import this Math class and System class with Static then to use pow()  sqrt() and println() methods, statement should be like this-

System.out.println(Math.sqrt( Math.pow(3,2)));  // square root of 3 * 3. output is 3

Now if we’ll import the classes System and Math with static as

import static java.lang.System.out;
import static java.lang.Math.*;

then  we can write the highlighted statement as

out.println(sqrt(pow(3,2)));    // output is 3


so if you import the classes static then you can use the static data types and static methods directly in to your java file.

Tuesday, September 23, 2014

Data member access table


You often get confused with the accessibility of data-members and methods in different classes , packages etc. So here is a table to help – class member access table. This table will help you to know , which type of class members(like- public , private, protected, default) are accessible in different conditions (like-  with-in same or different package/class and subclass or non subclass )


Condition

private
default
protected
public
With-in same class

 Y       
 Y
 Y
 Y
Same package and non-subclass
No
 Y
 Y
 Y
Same package and subclass
No              
 Y    
 Y
 Y
Different package and subclass
No
No
 Y
 Y
Different package and non-subclass
No
No
No
 Y

Y stands for 'yes'. 

All the four data members can be accessed anywhere with-in the same class.

If a single package contains different classes which are non-subclasses then private member of one class can’t be accessed in other class.

If a single package contains different classes and some of them are subclasses then private member of super class can’t be accessed in its subclasses classes/ derived classes.

If we have two or more packages and there is some inter-relation (inheritance) in between the classes of these packages then only protected and public data-member and methods are accessible.  

If we have two or more packages then only private members of classes can be accessed by the other classes of other packages.