SlideShare a Scribd company logo
JAVA ESSENTIAL TRAINING

          Abstract Class
          Interface
                                  Linh LeVan
                        lelinh2302@gmail.com
             http://tinhocbk123.appspot.com/



                                               1
&
VC
     BB
           Abstract Class

     An abstract method is a method that is declared
      without an implementation (without braces, and
      followed by a semicolon), like this:
            public abstract double getArea();
     An abstract class is a class that is
      declared abstract - it may or may not include
      abstract methods.
     Abstract classes cannot be instantiated, but they
      can be sub-classed.


                                                        2
&
VC
     BB
             Abstract Class

     If a class includes abstract methods,
      the class itself must be declared abstract, as in:
          public abstract class GraphicObject
          {
          //declare fields // declare non-abstract
          methods abstract void draw();
          }




                                                           3
&
VC
     BB
              Extending an Abstract Class

     public abstract class LivingThing {


          public void breath() {

              System.out.println("Living Thing breathing...");

          }

          public void eat() {

              System.out.println("Living Thing eating...");

          }

          public abstract void walk();
     }
                                                                 4
&
VC
     BB
           Extending an Abstract Class

     When a concrete class extends the LivingThing
     abstract class, it must implement the
     abstract method walk(), or else, that subclass will
     also become an abstract class, and therefore
     cannot be instantiated. For example,
     public class Human extends LivingThing {
       public void walk() {
          System.out.println("Human walks...");
       }
     }


                                                           5
&
VC
     BB
           Exercises

     Create an abstract class Figure having variables
     dim1,dim2 of double type and an abstract method
     area, then make two subclasses Rectangle and
     Triangle which will implement the area method.
     Create the abstract class reference variable,
     assign subclass objects to it and print the
     corresponding area.




                                                        6
&
VC
     BB




      JAVA ESSENTIAL TRAINING


      INTERFACE
                                7
&
VC
     BB
           Multiple inheritance problem!


     When we use the multiple inheritance, the child
      class object can access all the methods of its
      parent class.
     Whenever we declare an object of child class
      the system automatically loads all the methods
      and variables of its superclass into that object.
      But user generally does not work with all the
      methods simultaneously.so why the object loads
      all methods ,why not only required methods and
      variables.

                                                      8
&
VC
     BB
           Interface solution

     For this drawback multiple inheritance is not
      included in java.so that we can declare only that
      much thing we require.
     Using interface, you can specify what a class
      must do, but not how it does it.




                                                          9
&
VC
     BB
             Defining an Interface

     An interface is defined much like a class:
     access interface name{
          return-type method-name1(parameter-list);
          return-type method-name2(parameter-list);
          type final-varname1 = value;
          type final-varname2 = value;
          // ...
          return-type method-nameN(parameter-list);
          type final-varnameN = value;
     }




                                                      10
&
VC
     BB
           Meaning…

     Here, access is either public or not used.
     When no access specified is included, then
      default access results, and the interface is only
      available to other members of the package in
      which it is declared.
     When it is declared as public, the interface can
      be used by any other code.




                                                          11
&
VC
     BB
           Meaning…

     Name is the name of the interface, and can be
      any valid identifier.
     Notice that the methods which are declared
      have no bodies. They end with a semicolon after
      the parameter list.
     They are, essentially, abstract methods; there
      can be no default implementation of any method
      specified within an interface.
     Each class that includes an interface must
      implement all of the methods. Variables can be
      declared inside of interface declarations.     12
&
VC
     BB
           Meaning…

     Each class that includes an interface must
      implement all of the methods.
     Variables can be declared inside of interface
      declarations. They are implicitly final and static,
      meaning they cannot be changed by the
      implementing class. They must also be initialized
      with a constant value.
     All methods and variables are implicitly public if
      the interface, itself, is declared as public.


                                                        13
&
VC
     BB
          An example of an interface definition

      public interface IGreeting {
          public static final int MAX_MEMBER = 20;
          public void sayHello();
      }




                                                     14
&
VC
     BB
           Implementing Interfaces

     Once an interface has been defined, one or
      more classes can implement that interface.
     To implement an interface, include the
      implements clause in a class definition, and then
      create the methods defined by the interface.

     access class classname [extends superclass]
     [implements interface [,interface...]]{
     // class-body
     }

                                                      15
&
 VC
      BB
             Example

public class EnglishGreeting implements
IGreeting{
    @Override
    public void sayHello() {
        System.out.println("Hello Guy!");
    }
}


Note :When you implement an interface method, it must be declared as public.
                                                                               16
&
VC
     BB
           References to Interfaces

     You can declare variables as object reference
      that use an interface rather than a class type
     When you call a method through one of these
      references, the correct version will be called
      based on the actual instance of the interface
      being referred to. This one of the key features of
      interface.




                                                       17
&
VC
     BB
              References to Interfaces (cont’d)

     Demo:

public class TestInterface {

          public static void main(String[] args) {

                IGreeting javaInterfaceExample =

                             new EnglishGreeting();

                javaInterfaceExample.sayHello();

          }

}
                                                      18
&
VC
     BB
            Interface vs. Abstract Class

      All methods of an Interface are abstract methods while
       some methods of an Abstract class are abstract methods
      Abstract methods of abstract class have abstract
       modifier
      An interface can only define constants while
       abstract class can have fields
      Interfaces have no direct inherited relationship with any
       particular class, they are defined independently
      Interfaces themselves have
       inheritance relationship among themselves
      If a class includes an interface but does not fully
       implement the methods defined by that interface, then
       that class must be declared as abstract.                  19
&
VC
     BB
          Interfaces Can Be Extended

     One interface can inherit another by use of the
      keyword extends. The syntax is the same as for
      inheriting classes.
     When a class implements an interface that
      inherits another interface, it must provide
      implementations for all methods defined within
      the interface inheritance chain.




                                                    20
&
VC
     BB
           Exercises

     Define an interface having one method that takes
     an integer parameter. For this method, provide two
     implementations: In the first one,just print the value
     and in the second one, print the square of
     the number. Try to call both the versions.




                                                         21

More Related Content

PPT
Java Programming - Abstract Class and Interface
PPT
Java interfaces & abstract classes
PPTX
Interfaces and abstract classes
PDF
8 abstract classes and interfaces
PDF
javainterface
PPT
Chapter 9 Interface
PPTX
Java interfaces
PDF
What are Abstract Classes in Java | Edureka
Java Programming - Abstract Class and Interface
Java interfaces & abstract classes
Interfaces and abstract classes
8 abstract classes and interfaces
javainterface
Chapter 9 Interface
Java interfaces
What are Abstract Classes in Java | Edureka

What's hot (20)

PPTX
Abstraction in java [abstract classes and Interfaces
PPTX
Abstract class and interface
PPT
Abstract class
PPT
Abstract class in java
PPT
Interface in java By Dheeraj Kumar Singh
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
PPT
PPT
Java interface
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
Java interfaces
PPT
9 abstract interface
PPTX
Interface in java
PPTX
Interfaces in java
PPTX
Abstract Class & Abstract Method in Core Java
PPT
Interfaces In Java
PPT
06 abstract-classes
PPTX
Abstract class and Interface
PPT
Poo java
PPTX
Java interfaces
PPTX
Polymorphism presentation in java
Abstraction in java [abstract classes and Interfaces
Abstract class and interface
Abstract class
Abstract class in java
Interface in java By Dheeraj Kumar Singh
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java interface
Interface in java ,multiple inheritance in java, interface implementation
Java interfaces
9 abstract interface
Interface in java
Interfaces in java
Abstract Class & Abstract Method in Core Java
Interfaces In Java
06 abstract-classes
Abstract class and Interface
Poo java
Java interfaces
Polymorphism presentation in java
Ad

Viewers also liked (11)

PDF
Introduction Slide Deck
PDF
UNEP year book 2012 | Emerging issues in our global enviroment
PDF
Telstra case studies
PDF
How SME's Can Become Sustainability Leaders
PDF
Riflessioni sulla distribuzione e punti di Sv-Olta
PDF
Av nov brasil 3
PPTX
Biometrics Technology In the 21st Century
PDF
GFAR evolution and GCARD 3: Implications for governance
PDF
Effects of Trichoderma sp improved composts on vegetable production in Sub-Sa...
PDF
Java OOP Programming language (Part 5) - Inheritance
PPTX
How Clean is your Surf Spot ?
Introduction Slide Deck
UNEP year book 2012 | Emerging issues in our global enviroment
Telstra case studies
How SME's Can Become Sustainability Leaders
Riflessioni sulla distribuzione e punti di Sv-Olta
Av nov brasil 3
Biometrics Technology In the 21st Century
GFAR evolution and GCARD 3: Implications for governance
Effects of Trichoderma sp improved composts on vegetable production in Sub-Sa...
Java OOP Programming language (Part 5) - Inheritance
How Clean is your Surf Spot ?
Ad

Similar to Abstract & Interface (20)

PPTX
INTERFACES. with machine learning and data
PPTX
it is the quick gest about the interfaces in java
PPTX
Chapter 7:Understanding Class Inheritance
PPTX
FINAL_DAY10_INTERFACES_roles and benefits.pptx
PPTX
Interfaces c#
PDF
Exception handling and packages.pdf
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
PPT
12.2 Abstract class and Interface.ppt
DOCX
Binding,interface,abstarct class
PPT
Inheritance
PPTX
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
PPTX
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
PPS
Interface
PPTX
Abstract Classes and Interfaces in oop.pptx
PDF
Interface
DOCX
Core java questions
PPTX
Lecture 18
PDF
Interfaces in java
PPTX
Java notes of Chapter 3 presentation slides
PDF
Session 6_Java Interfaces_Details_Programs.pdf
INTERFACES. with machine learning and data
it is the quick gest about the interfaces in java
Chapter 7:Understanding Class Inheritance
FINAL_DAY10_INTERFACES_roles and benefits.pptx
Interfaces c#
Exception handling and packages.pdf
21UCAC31 Java Programming.pdf(MTNC)(BCA)
12.2 Abstract class and Interface.ppt
Binding,interface,abstarct class
Inheritance
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
Interface
Abstract Classes and Interfaces in oop.pptx
Interface
Core java questions
Lecture 18
Interfaces in java
Java notes of Chapter 3 presentation slides
Session 6_Java Interfaces_Details_Programs.pdf

More from Linh Lê (17)

PDF
Nq bq-nd ro
DOCX
LAB - He thong ten mien (DNS)
DOCX
Network Address Translation (NAT)
DOCX
nslookup - Quan tri mang (2)
PDF
Homework - C programming language
PDF
Access BaiGiang
PDF
LTC File
PDF
Kiểu cấu trúc và kiểu hợp
PDF
Các lệnh cơ bản
PDF
Word 2007 Labs
PDF
Kiểu mảng
PPTX
LTC-Cấu trúc rẽ nhánh
PDF
Xâu kí tự
PDF
Tong quan ve lap trinh
PDF
Collections
PDF
Cấu trúc lặp (loop)
PDF
Hàm (function)
Nq bq-nd ro
LAB - He thong ten mien (DNS)
Network Address Translation (NAT)
nslookup - Quan tri mang (2)
Homework - C programming language
Access BaiGiang
LTC File
Kiểu cấu trúc và kiểu hợp
Các lệnh cơ bản
Word 2007 Labs
Kiểu mảng
LTC-Cấu trúc rẽ nhánh
Xâu kí tự
Tong quan ve lap trinh
Collections
Cấu trúc lặp (loop)
Hàm (function)

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Sports Quiz easy sports quiz sports quiz
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
2.FourierTransform-ShortQuestionswithAnswers.pdf
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Sports Quiz easy sports quiz sports quiz

Abstract & Interface

  • 1. JAVA ESSENTIAL TRAINING Abstract Class Interface Linh LeVan lelinh2302@gmail.com http://tinhocbk123.appspot.com/ 1
  • 2. & VC BB Abstract Class An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: public abstract double getArea(); An abstract class is a class that is declared abstract - it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub-classed. 2
  • 3. & VC BB Abstract Class If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { //declare fields // declare non-abstract methods abstract void draw(); } 3
  • 4. & VC BB Extending an Abstract Class public abstract class LivingThing { public void breath() { System.out.println("Living Thing breathing..."); } public void eat() { System.out.println("Living Thing eating..."); } public abstract void walk(); } 4
  • 5. & VC BB Extending an Abstract Class When a concrete class extends the LivingThing abstract class, it must implement the abstract method walk(), or else, that subclass will also become an abstract class, and therefore cannot be instantiated. For example, public class Human extends LivingThing { public void walk() { System.out.println("Human walks..."); } } 5
  • 6. & VC BB Exercises Create an abstract class Figure having variables dim1,dim2 of double type and an abstract method area, then make two subclasses Rectangle and Triangle which will implement the area method. Create the abstract class reference variable, assign subclass objects to it and print the corresponding area. 6
  • 7. & VC BB JAVA ESSENTIAL TRAINING INTERFACE 7
  • 8. & VC BB Multiple inheritance problem! When we use the multiple inheritance, the child class object can access all the methods of its parent class. Whenever we declare an object of child class the system automatically loads all the methods and variables of its superclass into that object. But user generally does not work with all the methods simultaneously.so why the object loads all methods ,why not only required methods and variables. 8
  • 9. & VC BB Interface solution For this drawback multiple inheritance is not included in java.so that we can declare only that much thing we require. Using interface, you can specify what a class must do, but not how it does it. 9
  • 10. & VC BB Defining an Interface An interface is defined much like a class: access interface name{ return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; } 10
  • 11. & VC BB Meaning… Here, access is either public or not used. When no access specified is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. 11
  • 12. & VC BB Meaning… Name is the name of the interface, and can be any valid identifier. Notice that the methods which are declared have no bodies. They end with a semicolon after the parameter list. They are, essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. 12
  • 13. & VC BB Meaning… Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public. 13
  • 14. & VC BB An example of an interface definition public interface IGreeting { public static final int MAX_MEMBER = 20; public void sayHello(); } 14
  • 15. & VC BB Implementing Interfaces Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. access class classname [extends superclass] [implements interface [,interface...]]{ // class-body } 15
  • 16. & VC BB Example public class EnglishGreeting implements IGreeting{ @Override public void sayHello() { System.out.println("Hello Guy!"); } } Note :When you implement an interface method, it must be declared as public. 16
  • 17. & VC BB References to Interfaces You can declare variables as object reference that use an interface rather than a class type When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This one of the key features of interface. 17
  • 18. & VC BB References to Interfaces (cont’d) Demo: public class TestInterface { public static void main(String[] args) { IGreeting javaInterfaceExample = new EnglishGreeting(); javaInterfaceExample.sayHello(); } } 18
  • 19. & VC BB Interface vs. Abstract Class  All methods of an Interface are abstract methods while some methods of an Abstract class are abstract methods  Abstract methods of abstract class have abstract modifier  An interface can only define constants while abstract class can have fields  Interfaces have no direct inherited relationship with any particular class, they are defined independently  Interfaces themselves have inheritance relationship among themselves  If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract. 19
  • 20. & VC BB Interfaces Can Be Extended One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. 20
  • 21. & VC BB Exercises Define an interface having one method that takes an integer parameter. For this method, provide two implementations: In the first one,just print the value and in the second one, print the square of the number. Try to call both the versions. 21