Interfaces and Abstract Class by Durga Sir
Interfaces and Abstract Class by Durga Sir
1. Introduction
2. Interfaces declaration and implementation
3. Extends V/S implementation
4. Interface methods
5. Interface variables
6. Interface naming conflicts
Method naming conflicts
Variable naming conflicts
7. Marker interface (most valuable for interview)
8. Adapter classes
9. Interfaces v/s abstract class v/s concrete classes
10. Difference between interface and abstract class(most valuable for
interview)
11. Conclusion
Introduction:
1. Definition1: Any service requirements specification (RSS) is
considered as an interface.
Example1:
JDBC API is access requirements specification to develop database
driver.
Database vendor is responsible to implement JDBC API.
SUN people are responsible to develop JDBC API and define JDBC API.
Database vendor is responsible to implement JDBC API.
Example2:
Servlet API is access requirements to develop web server.
Web server vendor is responsible to implement Servlet API.
Web Application
Interface
Example:
Through bank ATM GUI screen, bank people highlighting the set of service they
are offering at the same time the same GUI screen represents the set of
services what customer is accepting. Hence this GUI screen access contract
between customer and bank people.
3. Definiton3:
Inside interface every method is always public and abstract whether we are
declaring the methods and abstract or not. Hence interface is considered as
100% pure abstract class.
4. Summary Definition:
package pack5;
package pack5;
package pack5;
}
public static void main(String[] args) {
SubServiceProvider s = new SubServiceProvider();
s.m1();
s.m2();
}
}
package pack5;
package pack5;
package pack4;
public interface A {
package pack4;
public interface B {
package pack4;
package pack4;
public interface A {
package pack4;
public interface B {
package pack4;
public interface C {
package pack4;
package pack4;
public interface A {
package pack4;
public interface B {
package pack4;
public interface C {
package pack4;
public class D {
}
package pack4;
}
Interface and methods
1. Every method present inside in an interface is always public and
abstract whether we are declaring or not.
2. Example:
package pack5;
Note:
void m1();
public void m1();
abstract void m1();
public abstract void m1();
package pack5;
Q: Why the variables inside interface are public, static and final?
Note:
Hence within the interface the following declaration of variables are
equal:
int x = 10;
public int x = 10;
static int x = 10;
final int x = 10;
public static int x = 10;
public final int x = 10;
static final int x = 10;
public static final int x = 10;
3. As every interface variable is always public static final.
We can’t declare with the following modifier:
package pack5;
package pack5;
package pack4;
import pack5.Interf;
package pack5;
public interface Interf {
int x = 10;
}
package pack4;
import pack5.Interf;
Case1:
If two interfaces contain a method with the same signature and same
return type then in the implementation class we have to provide
implementation for only one method.
package pack5;
interface B {
void m1();
}
package pack4;
interface A {
void m1();
package pack4;
@Override
public void m1() {
System.out.println(1);
}
}
Case2:
If two methods contain a method with same name but different argument type
then in the implementation class we have to provide implementation for
methods and these methods as said overloaded methods.
package pack5;
public interface B {
void m1();
package pack4;
interface A {
void m1(int x);
package pack4;
import pack5.B;
@Override
public void m1() {
System.out.println(1);
} Overloaded methods
@Override
public void m1(int x) {
System.out.println(x);
}
public static void main(String[] args) {
D d = new D();
d.m1();
d.m1(10);
}
}
Case3:
If two interfaces contain a method with the same signature but return types
then it is impossible to implement both interfaces simultaneously (if return
types are not co-variant).
package pack4;
interface A {
void m1();
package pack5;
public interface B {
int m1();
package pack4;
import pack5.B;
Yes, except a particular case. If two interfaces contain method with the same
signature but return type then it is important to implement both interfaces
simultaneously.
Interface Variables naming conflicts:
1. Two interfaces can contain a variable with the same name and they
may be a chance of variable naming conflicts but we can solve
this problem by using interface names.
package pack4;
interface A {
int x = 111;
package pack5;
public interface B {
int x = 222;
package pack4;
import pack5.B;
}
Marker Interface/ Ability interface/ Tag
interface:
1. If an interface doesn’t contain any methods and by implementing
that interface if our objects will get some ability. Such types
of interfaces are called marker interfaces.
Example:
Serializable interface
Cloneable interface These are marked for some ability
RandomAccess interface
Single thread model interface
Example1:
Serializable interface:
Example2:
Cloneable interface:
Q: Without having any methods, how the objects get some ability in marker
interfaces?
package pack4;
interface A {
void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();
package pack4;
abstract class Adapterx extends A{
void m1() { }
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };
package pack4;
interface A {
void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();
package pack4;
abstract class Adapterx extends A{
void m1() {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };
The problem in this approach is, it increases length of the code and reduces
readability.
package pack4;
interface A {
void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();
package pack4;
abstract class Adapterx extends A{
void m1(){ }
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };
package pack4;
}
public void m1000(){
}
}
Https Servlet:
Conclusion:
Note:
Example: MyOwnServlet
Example:
package pack6;
String name;
int age;
long mobileNo;
String address;
String collegeName;
String marriedStatus;
int homeNo;
package pack6;
this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;
this.marks = marks;
System.out.println(this.name);------------- Lalit
System.out.println(this.age);-------------- 26
System.out.println(this.mobileNo);--------- 999999999
System.out.println(this.address);---------- Panipat
System.out.println(this.collegeName);------ CDLU
System.out.println(this.marriedStatus);---- No
System.out.println(this.homeNo);----------- 220
System.out.println(this.marks);------------ 100
}
}
}
package pack6;
this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;
this.subject = subject;
System.out.println(this.name);----------------- Durga
System.out.println(this.age);------------------ 40
System.out.println(this.mobileNo);------------- 88888880
System.out.println(this.address);-------------- Banglore
System.out.println(this.collegeName);---------- VIT
System.out.println(this.marriedStatus);-------- Yes
System.out.println(this.homeNo);--------------- 110
System.out.println(this.subject);-------------- JAVA
System.out.println(t.name);------------------- Durga
System.out.println(t.age);-------------------- 40
System.out.println(t.mobileNo);--------------- 88888880
System.out.println(t.address);---------------- Banglore
System.out.println(t.collegeName);------------ VIT
System.out.println(t.marriedStatus);---------- Yes
System.out.println(t.homeNo);----------------- 110
System.out.println(t.subject);---------------- JAVA
}
}
package pack6;
this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;
package pack6;
System.out.println(this.name);------------------------ Lalit
System.out.println(this.age);------------------------- 26
System.out.println(this.mobileNo);-------------------- 999999999
System.out.println(this.address);--------------------- Panipat
System.out.println(this.collegeName);----------------- CDLU
System.out.println(this.marriedStatus);--------------- No
System.out.println(this.homeNo);---------------------- 220
System.out.println(this.marks);----------------------- 100
}
System.out.println(s.name);----------------------- Lalit
System.out.println(s.age);------------------------ 26
System.out.println(s.mobileNo);------------------- 999999999
System.out.println(s.address);-------------------- Panipat
System.out.println(s.collegeName);---------------- CDLU
System.out.println(s.marriedStatus);-------------- No
System.out.println(s.homeNo);--------------------- 220
System.out.println(s.marks);---------------------- 100
}
}
package pack6;
this.subject = subject;
System.out.println(this.name);-------------------- Durga
System.out.println(this.age);--------------------- 40
System.out.println(this.mobileNo);---------------- 88888880
System.out.println(this.address);----------------- Banglore
System.out.println(this.collegeName);------------- VIT
System.out.println(this.marriedStatus);----------- Yes
System.out.println(this.homeNo);------------------ 110
System.out.println(this.subject);----------------- JAVA
System.out.println(t.name);------------------- Durga
System.out.println(t.age);-------------------- 40
System.out.println(t.mobileNo);--------------- 88888880
System.out.println(t.address);---------------- Banglore
System.out.println(t.collegeName);------------ VIT
System.out.println(t.marriedStatus);---------- Yes
System.out.println(t.homeNo);----------------- 110
System.out.println(t.subject);---------------- JAVA
}
}
Note:
Ans:
Abstract class can contain instance variables which are required for
child object. To perform initialization of those instance variables
constructor is required for abstract class.
3. Whenever we are creating child class object parent class object won’t
be created. Just parent class constructor will be executed for the
child class object purpose only.
Example:
package pack6;
P(){
System.out.println(this.hashCode());
}
}
package pack6;
public C() {
System.out.println(this.hashCode());
}
package pack6;
Output:
366712642
366712642
366712642
Q: Inside interface every method is always abstract and we can take only
abstract methods in abstract class also then what is the difference between
interface and abstract class i.e Is it possible to replace with abstract
class?
Ans: We can replace interface with abstract class but it is not a good
programming practice. This is something like recruiting IAS Officer for
Sweeping activity.
package pack6;
P(String name){
this.name = name;
System.out.println(this.name);
System.out.println(1);
}
package pack6;
Test(String surName) {
super("Lalit");
this.surName = surName;
System.out.println(this.surName);
}
@Override
public void m1() {
System.out.println(2);
}
System.out.println(t.name);
System.out.println(t.surName);
t.m1();
}
package pack6;
interface C {
package pack6;
@Override
public void m2() {
System.out.println(1);
}
@Override
public void m3() {
System.out.println(2);
}
}
Approach1 Approach2
1. We can do inherit only once class 1. We can do inherit one class and
and not other classes. So here multiple interfaces simultaneously.
inheritance problem is created. So here inheritance concept is more
useful.
2. Here child class object created 2. Here child class object created
and then parent constructor will be and direct execute interface methods
executed by child class object only. due to overriding(after implementing
So here program will take more time in child class). So here program will
to run. take less time.
Note:
Child class
by child constructor
package pack6;
System.out.println(this.name);
System.out.println(this.age);
}
}
package pack6;
System.out.println(this.rollNo);
System.out.println(this.name);
System.out.println(this.age);
System.out.println(this.marks);
}
In the above program both parent and child constructor executed for
child object initialization only.
package pack6;
package pack6;
Student(){
super();
System.out.println(this.hashCode());
}
package pack6;
Output:
366712642
366712642
366712642