Different types of classes in java part 2

tech channel with DSK
3 min readMar 14, 2021

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?

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 { 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[]) { }

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; } AnotherDefaultClass() {} } // AnotherDefaultClassSubClass.java class AnotherDefaultClassSubClass extends AnotherDefaultClass { int k,l; /* 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); } }

--

--

tech channel with DSK
0 Followers

Hi there , I am kashyap I have started writing this posts inorder to give more information on programming languages and my blog is useful for both ECE and CSE