Topics

6/recent/ticker-posts

Different types of classes in java part 2

Types Of Classes  :

Abstract class

  • If class contains abstract methods then it must be declared as abstract.
  • Abstract class may or may not contain abstract methods.


Without abstract methods why class is declared as abstract?

  • If class don't want to be instantiated 


Classes which contain common method implementations to all its sub classes are declared as abstract such as Component class is Java AWT/Swings, HttpServlet in servlet API.


Component sub classes are:

Label, Button, TextField, TextArea, List, Checkbox, CheckboxGroup, H/V Scrollbar, Drop down list


Properties that are commonly required in all component sub classes are:

  • x, y, width, height, background, foreground, text, visible, enable, editable, resizeable. These properties needs one pair of setter and getter methods. 
  • All together 22 methods are required. These 22 methods implemented in 10 component subclasses together comes to 220 methods.
  •  Instead of implementing all 22 methods in all the sub classes. These methods are defined only in one single Component class. 
  • Component class itself don't want to be instantiated. Hence it is declared as abstract.


Though generic method implementations coming from abstract super class, if child classes want to override they can override super class methods.


E:\active\core\am3\javalang\oops\abstraction inheritance polymorphism\abstract class

// AbstractClass.java

public abstract AbstractClass {

    // private abstract void print(String name); 

// illegal combination of modifiers: abstract and private

    protected abstract void print(String name);

}


// AbstractClassSubClass.java

public class AbstractClassSubClass extends AbstractClass {

   public void print(String name) {

      System.out.println("print() method from AbstractClassSubClass name is "+name);

   }

   

   // weaker access privilege error comes because super class method is protected, it can go upto sub class outside the package

   /* void print(String name) {

      System.out.println("print() method from AbstractClassSubClass name is "+name);

   }*/

}


// AbstractClassClient.java

public class AbstractClassClient {

    public static void main(String rags[]) {

   AbstractClass ac=AbstractClassSubClass();

   ac.print("Nani");

}

}


class

  • To class we can apply default or public access specifier.
  • To access modifier is by default applied to class.
  • All methods are implemented in class.
  • Class can be both inherited and as well as instantiated.


If class is written with default access specifier during compilation compiler will generate one default constructor in the class.

  • default constructor always generated with no parameters and its access specifier is as same as class access specifier that means if class declared with default access specifier constructor is also generated with default access specifier. 
  • If class is declared as public then its constructor is also generated with public access specifier.


// DefaultClass.java

class DefaultClass {

  int i, j;

}


// public Class.java

public class PublicClass {

  int i, j;

}


// AnotherDefaultClass.java

class AnotherDefaultClass {

  int i, j;

  

  // no default constructor is generated by compiler

  

  // constructor with 2 parameters

  AnotherDefaultClass(int a, int b)  {

    this.i=a;

    this.j=b;

  }

  

  // if we explicitly generate a constructor no parameter constructor, then default constructor generation is possible in sub class.

  AnotherDefaultClass() {}

}


// AnotherDefaultClassSubClass.java

class AnotherDefaultClassSubClass extends AnotherDefaultClass {


    int k,l;

   // before this if no parameter constructor doesn't exist in the super class

   /* when we dont write any constructor, one default constructor is generated that will implcitly call super(), then during compilation compiler say cannot find symbol AnotherDefaultClass() */

   

   // no parameter constructor

   AnotherDefaultClassSubClass() {

      super();

   }

   

   // two parameter constructor

   AnotherDefaultClassSubClass(int a, int b) {

      super(a,b);

   }

   

   // four parameter constructor

   AnotherDefaultClassSubClass(int a, int b, int c, int d) {

      super(a,b);

  k=c;

  l=d;

   }

}


Post a Comment

0 Comments