Java Programming Manish Kumar (manish87it@gmail.com) Lecture- 8 Abstract class & Interface
Contents  Introduction to Abstract Class  Upcasting/Dynamic Dispatch Method  Importance of Abstract Class (Using real life based program)  Abstract Class Example (with constructor, methods and variables)  Introduction to Interface  Interface implementations  Marker and Nested Interface  Abstract Class Vs. Interface
Abstract class  A class declared with “abstract” keyword is called abstract class. An abstract class may or may not contain an “abstract method”.  A method that cannot have definition (i.e., method body) part is known as abstract method.  There are some important points of an abstract class as follows: 1. An abstract class must be declared with abstract keyword. 2. It contain both or none i.e., abstract and non-abstract method. 3. We cannot create an object of an abstract class. Declaration of an abstract class and abstract method- abstract class Abc {} Here abstract is a keyword that is used to make a class as an abstract class. abstract void display(); As you see above method declaration, there is no body of the method and declared with abstract keyword.
Example-1 abstract class Polygon { abstract void area(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(); } } Tringle.java Output – Area of tringle = 314.0
Example-2 Note: - When an abstract class is inherited, then derived class must provide the implementations for all the abstract method declared in base class otherwise it gives an error at compile time. Tringle.java abstract class Polygon { abstract void area(); abstract void display(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); // Upcasting p.area(); } } Output- error: Tringle is not abstract and does not override abstract method display() in Polygon class Tringle extends Polygon { ^ 1 error
upcasting Upcasting/Dynamic Dispatch Method When an object of derived class is assigned to base class reference variable, then it is called Upcasting. In previous slide’s example – p is a reference variable of the base class and it assigned by an object of derived class. In this case, calling method first search the existence of method in base class then it will execute the method in derived class. Polygon p = new Tringle(); // Upcasting
Importance of abstract class //Bank.java abstract class RBI { abstract int getROI(); } class SBI extends RBI { int getROI() { return 7; }} class PNB extends RBI { int getROI() { return 8; }} class Bank { public static void main(String args[]) { RBI rb; rb = new SBI(); System.out.println("ROI of SBI(%) = "+rb.getROI()); rb = new PNB(); System.out.println("ROI of PNB(%) = "+rb.getROI()); } } Output – ROI of SBI(%) = 7 ROI of PNB(%) = 8
example AbstractClassExample.java (constructor, variables and methods): abstract class Polygon{ Polygon() { System.out.println("Polygon created."); } abstract void area(); void display() { System.out.println("This is non-abstract method"); }} class Rectangle extends Polygon { void area() { int l=10,w=7, area; area = l*w; System.out.println("Area of Rectangle = "+area); } } class AbstractClassExample { public static void main(String args[]) { Polygon p =new Rectangle(); p.area(); p.display(); } } Output – Polygon created. Area of Rectangle = 70 This is non-abstract method
interface An interface is declared with interface keyword. We can achieve 100% abstraction using interface. It can be defined as “the collection of public, static, final data member and abstract methods.” There are few points that need to remembered: 1. Interface declaration is just like class; only difference is that it uses the keyword interface in place of class. 2. The method in interface is by default public and abstract but since java 9; we can have private method in an interface and since java 8; interface can have default method. 3. The data member in interface is by default public, static and final. 4. It also represents the IS-A relationship. 5. We can support multiple inheritance using interface. 6. We cannot create an object of an interface.
declaration Interface <interface-name> { //declare data member and methods } Declaring an interface: Relationship between class and interface •A class can implements an interface or more than one interface. •An interface can extends another interface. •An interface can also implements multiple interface.
Example - 1 interface Animal { void livingPlace(); } class Lion implements Animal { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public static void main(String args[]) { Animal an = new Lion(); an.livingPlace(); } } Output- The living place of Lion is Forest. Lion.java A class can implements an interface.
Example-2 Lion.java interface Animal { void livingPlace(); } interface Birds { void live(); } class Lion implements Animal, Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Animal an= new Lion(); an.livingPlace(); Birds b = new Lion(); b.live(); } } Output- The living place of Lion is Forest. The living place of birds in Nest. A class can implements an interface or more than one interface.
Example-3 interface Animal { void livingPlace(); } interface Birds extends Animal { void live(); } class Lion implements Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Birds b = new Lion(); b.livingPlace(); b.live(); } } Lion.java Output- The living place of Lion is Forest. The living place of birds in Nest. An interface can extends another interface.
Example-4 interface Polygon { int a = 10; void area(int x); } class Tringle implements Polygon { public void area(int x) { double pi = 3.14; double area = pi*x*x; System.out.println("Area of tringle = "+area); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(10); } } Output- Area of tringle = 314.0 When you override the abstract method declared in interface it should be noted that overridden method must be public because the method in interface is by default public. When you declare a variable in interface then it is mandatory to initialize because it is by default; public, static and final.
Marker & nested interface An empty interface is called marker interface i.e. an interface without data member and methods called Marker Interface. For example – Cloneable, Serializable, etc. They are used to provide essential information to the JVM. public interface Serializable { } Nested Interface An interface inside another interface is known as Nested Interface. Interface Abc { void show(); interface Xyz { void display(); } }
Abstract class vs interface Abstract Class Interface Abstract class may have abstract and non-abstract method Interface can have only abstract method Multiple inheritance not supported by abstract class. Interface supports multiple inheritance. Abstract class can have static, non-static, final, non-final variables. Interface can have only static and final variables. abstract keyword is used to declare a class as an Abstract class. interface keyword is used to declare an interface. An abstract class can be extended using “extends” keyword. An interface can be implemented using “implements” keyword. We can achieve 0 to 100% abstraction. Achieve 100% abstraction. Abstract class can have “private, protected, etc.” class member. Members of an interface are public and default only.
Lecture 8 abstract class and interface

Lecture 8 abstract class and interface

  • 1.
  • 2.
    Contents  Introduction toAbstract Class  Upcasting/Dynamic Dispatch Method  Importance of Abstract Class (Using real life based program)  Abstract Class Example (with constructor, methods and variables)  Introduction to Interface  Interface implementations  Marker and Nested Interface  Abstract Class Vs. Interface
  • 3.
    Abstract class  Aclass declared with “abstract” keyword is called abstract class. An abstract class may or may not contain an “abstract method”.  A method that cannot have definition (i.e., method body) part is known as abstract method.  There are some important points of an abstract class as follows: 1. An abstract class must be declared with abstract keyword. 2. It contain both or none i.e., abstract and non-abstract method. 3. We cannot create an object of an abstract class. Declaration of an abstract class and abstract method- abstract class Abc {} Here abstract is a keyword that is used to make a class as an abstract class. abstract void display(); As you see above method declaration, there is no body of the method and declared with abstract keyword.
  • 4.
    Example-1 abstract class Polygon{ abstract void area(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(); } } Tringle.java Output – Area of tringle = 314.0
  • 5.
    Example-2 Note: - Whenan abstract class is inherited, then derived class must provide the implementations for all the abstract method declared in base class otherwise it gives an error at compile time. Tringle.java abstract class Polygon { abstract void area(); abstract void display(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); // Upcasting p.area(); } } Output- error: Tringle is not abstract and does not override abstract method display() in Polygon class Tringle extends Polygon { ^ 1 error
  • 6.
    upcasting Upcasting/Dynamic Dispatch Method Whenan object of derived class is assigned to base class reference variable, then it is called Upcasting. In previous slide’s example – p is a reference variable of the base class and it assigned by an object of derived class. In this case, calling method first search the existence of method in base class then it will execute the method in derived class. Polygon p = new Tringle(); // Upcasting
  • 7.
    Importance of abstractclass //Bank.java abstract class RBI { abstract int getROI(); } class SBI extends RBI { int getROI() { return 7; }} class PNB extends RBI { int getROI() { return 8; }} class Bank { public static void main(String args[]) { RBI rb; rb = new SBI(); System.out.println("ROI of SBI(%) = "+rb.getROI()); rb = new PNB(); System.out.println("ROI of PNB(%) = "+rb.getROI()); } } Output – ROI of SBI(%) = 7 ROI of PNB(%) = 8
  • 8.
    example AbstractClassExample.java (constructor, variables andmethods): abstract class Polygon{ Polygon() { System.out.println("Polygon created."); } abstract void area(); void display() { System.out.println("This is non-abstract method"); }} class Rectangle extends Polygon { void area() { int l=10,w=7, area; area = l*w; System.out.println("Area of Rectangle = "+area); } } class AbstractClassExample { public static void main(String args[]) { Polygon p =new Rectangle(); p.area(); p.display(); } } Output – Polygon created. Area of Rectangle = 70 This is non-abstract method
  • 9.
    interface An interface isdeclared with interface keyword. We can achieve 100% abstraction using interface. It can be defined as “the collection of public, static, final data member and abstract methods.” There are few points that need to remembered: 1. Interface declaration is just like class; only difference is that it uses the keyword interface in place of class. 2. The method in interface is by default public and abstract but since java 9; we can have private method in an interface and since java 8; interface can have default method. 3. The data member in interface is by default public, static and final. 4. It also represents the IS-A relationship. 5. We can support multiple inheritance using interface. 6. We cannot create an object of an interface.
  • 10.
    declaration Interface <interface-name> { //declaredata member and methods } Declaring an interface: Relationship between class and interface •A class can implements an interface or more than one interface. •An interface can extends another interface. •An interface can also implements multiple interface.
  • 11.
    Example - 1 interfaceAnimal { void livingPlace(); } class Lion implements Animal { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public static void main(String args[]) { Animal an = new Lion(); an.livingPlace(); } } Output- The living place of Lion is Forest. Lion.java A class can implements an interface.
  • 12.
    Example-2 Lion.java interface Animal { voidlivingPlace(); } interface Birds { void live(); } class Lion implements Animal, Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Animal an= new Lion(); an.livingPlace(); Birds b = new Lion(); b.live(); } } Output- The living place of Lion is Forest. The living place of birds in Nest. A class can implements an interface or more than one interface.
  • 13.
    Example-3 interface Animal { voidlivingPlace(); } interface Birds extends Animal { void live(); } class Lion implements Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Birds b = new Lion(); b.livingPlace(); b.live(); } } Lion.java Output- The living place of Lion is Forest. The living place of birds in Nest. An interface can extends another interface.
  • 14.
    Example-4 interface Polygon { inta = 10; void area(int x); } class Tringle implements Polygon { public void area(int x) { double pi = 3.14; double area = pi*x*x; System.out.println("Area of tringle = "+area); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(10); } } Output- Area of tringle = 314.0 When you override the abstract method declared in interface it should be noted that overridden method must be public because the method in interface is by default public. When you declare a variable in interface then it is mandatory to initialize because it is by default; public, static and final.
  • 15.
    Marker & nestedinterface An empty interface is called marker interface i.e. an interface without data member and methods called Marker Interface. For example – Cloneable, Serializable, etc. They are used to provide essential information to the JVM. public interface Serializable { } Nested Interface An interface inside another interface is known as Nested Interface. Interface Abc { void show(); interface Xyz { void display(); } }
  • 16.
    Abstract class vsinterface Abstract Class Interface Abstract class may have abstract and non-abstract method Interface can have only abstract method Multiple inheritance not supported by abstract class. Interface supports multiple inheritance. Abstract class can have static, non-static, final, non-final variables. Interface can have only static and final variables. abstract keyword is used to declare a class as an Abstract class. interface keyword is used to declare an interface. An abstract class can be extended using “extends” keyword. An interface can be implemented using “implements” keyword. We can achieve 0 to 100% abstraction. Achieve 100% abstraction. Abstract class can have “private, protected, etc.” class member. Members of an interface are public and default only.