Different types of Classes in Java

tech channel with DSK
4 min readMar 13, 2021

i) Pure abstract class/interface ii) Abstract class iii) Class iv) Final class v) Inner class vi) Anonymous class vii) Enum

interface:

DEF: interface is also similar to a class except in interface all the methods are abstract. No method is found with implementations in interface. interface must be created using ‘interface’ keyword at the beginning followed by interface name. interface must be enclosed in flower braces. Two imp points: // MyInterface.java abstract interface MyInterface { int min_age; void method1() {} } javac MyInterface.java compilation error comes saying that interface variables are final so that min_age variable must be initialized and also says interface cannot contain implemented methods. // MyInterface.java interface MyInterface { int MIN_AGE=18; void method1(); } javac MyInterface.java The .java file is compiled and .class file is produced. In that .class file we will notice // MyInterface.java abstract interface MyInterface { public static final int MIN_AGE=18; public abstract void method1(); } In interface all variables are by default public static final and all methods are public abstract by default. Why interface variables are final by default because in multiple inheritance super class variable ambiguity problem arises. How? class A { } interface B { } } // a class extending from multiple super classes is multiple inheritance. Like in C++ class D extends A, B, C { } // In multiple inheritance, implemented methods are derived from multiple super classes // In java we are getting implemented methods from only one super class and getting declared / abstract methods from multiple super interfaces. Hence java is multi-level inheritance based classes. // following is a multi-level inheritance class D extends A implements B, C { System.out.println(x); // 14 System.out.println(this.x); // 13 System.out.println(super.x); //10 System.out.println(B.x); // 11 System.out.println(C.x); // 12 }

  • Suppose if class E derives from A,B,C,D classes in which a variable is declared commonly in all the super classes initialized with different values.
  • E class is also having a variable called a. While compiling complier complies E successfully because E class a variable can be called with this.a and a variable coming from four super classes one variable reinitializes other variable.
  • But while running java program if we invoke super.a then runtime environment detects they are four super classes found and ‘a’ variable is ambiguously found in all the super classes, runtime environment gives error at runtime.
  • But since java supports multi-level but not multiple inheritance we can extend from only one super class at a time and can inherit from multiple interfaces at a time.
  • If class E derives from A class and implement from B,C,D interfaces. Compiler accepts that ‘a’ variable derived from A is reinitialized with ‘a’ variable derived from B, that to super class variable becomes final if the same variable tries to be reinitialized deriving from C and D, compiler gives an error saying final variable derived from super class cannot be reinitialized because it is already a final variable.
  • Hence we must avoid inheriting E from C and D or change the variable names in C and D.

Why interface variables are static too? There are 2 reasons: Because if final variable is not declared as static as many instances of interface are created (mean sub classes of interface are instantiated) in those many instances final variable memory will be allocated. Because interfaces cannot be instantiated its variable can be accessed via directly by calling interfacename.variablename because they are static. Why interface methods are abstract by default? a) You write a class with 5 methods implementation Declare 3 methods as abstract in one interface and declare the remaining two methods in one more interface. Give interface1 to one client using which the user can access only 3 methods of implementation class but not the other two. Give interface2 to one more client using which the second user can access only the last methods implementation class. E:\active\core\am3\javalang\oops\abstraction inheritance polymorphism\interface // interface1.java interface interface1 { public void method1(); public void method2(); public void method3(); } // interface2.java interface interface2 { public void method4(); public void method5(); } Why interface is named as interface? With the help of interfaces a class can have many faces. // InterfaceImplementationClass.java class InterfaceImplementationClass implements interface1, interface2 { public void method1() { System.out.println(“method1() called”); } public void method2() { System.out.println(“method2() called”); } public void method3() { System.out.println(“method3() called”); } public void method4() { System.out.println(“method4() called”); } public void method5() { System.out.println(“method5() called”); } } // Client1.java public class Client1 { public static void main(String rags[]) { interface1 i1=new InterfaceImplementationClass(); i1.method1(); i1.method2(); i1.method3(); // i1.method5(); // cannot find symbol } } // Client2.java public class Client2 { public static void main(String rags[]) { interface2 i2=new InterfaceImplementationClass(); // i2.method2(); // cannot find symbol // i2.method3(); // cannot find symbol i2.method4(); i2.method5(); } } super class forcing to implement methods in subclass: Why super class forces sub classes to implement their methods is to maintain uniform number of methods in all sub classes. Because of this feature java programs became vendor independent. For example our java classes sometimes runs on other runtime environments such as web server, application server, DB etc. If the server expects some methods on our to run them at runtime. Webserver indirectly forces us to implement from an interface, unless we implement all the methods derived from interface our class cannot be compiled. For example Servlet, Runnable, Cloneable etc.

runtime polymorphism:

EmpPayroll, Admin, Sales,ProdEmpPayroll classes. Printer, STDOUTPrinter, FilePrinter Emp, EmpV1, EmpV2, EmpV3 etc

Reducing method overloading burden:

Outputter class print() method takes Printer interface type object as argument. The Printer interface sub classes may be STDOUTPrinter, File Printer, DB Printer, Socket Printer, Printer Printer, Cloud Printer

--

--

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