SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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

Weitere ähnliche Inhalte

Was ist angesagt?

Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interfaceMazharul Sabbir
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 

Was ist angesagt? (20)

Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java interface
Java interfaceJava interface
Java interface
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
9 abstract interface
9 abstract interface9 abstract interface
9 abstract interface
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Poo java
Poo javaPoo java
Poo java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 

Andere mochten auch

Andere mochten auch (11)

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

Ähnlich wie Abstract & Interface

Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceIt Academy
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.pptVISHNUSHANKARSINGH3
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct classvishal choudhary
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfTabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptTabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptTabassumMaktum
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 

Ähnlich wie Abstract & Interface (20)

Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class Inheritance
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
 
Inheritance
InheritanceInheritance
Inheritance
 
Interface
InterfaceInterface
Interface
 
Interface
InterfaceInterface
Interface
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Interfaces
InterfacesInterfaces
Interfaces
 
Java 06
Java 06Java 06
Java 06
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
JAVA.pptx
JAVA.pptxJAVA.pptx
JAVA.pptx
 

Mehr von Linh Lê

Nq bq-nd ro
Nq bq-nd roNq bq-nd ro
Nq bq-nd roLinh Lê
 
LAB - He thong ten mien (DNS)
LAB - He thong ten mien (DNS)LAB - He thong ten mien (DNS)
LAB - He thong ten mien (DNS)Linh Lê
 
Network Address Translation (NAT)
Network Address Translation (NAT)Network Address Translation (NAT)
Network Address Translation (NAT)Linh Lê
 
nslookup - Quan tri mang (2)
nslookup - Quan tri mang (2)nslookup - Quan tri mang (2)
nslookup - Quan tri mang (2)Linh Lê
 
Homework - C programming language
Homework - C programming languageHomework - C programming language
Homework - C programming languageLinh Lê
 
Access BaiGiang
Access BaiGiangAccess BaiGiang
Access BaiGiangLinh Lê
 
Kiểu cấu trúc và kiểu hợp
Kiểu cấu trúc và kiểu hợpKiểu cấu trúc và kiểu hợp
Kiểu cấu trúc và kiểu hợpLinh Lê
 
Các lệnh cơ bản
Các lệnh cơ bảnCác lệnh cơ bản
Các lệnh cơ bảnLinh Lê
 
Word 2007 Labs
Word 2007 LabsWord 2007 Labs
Word 2007 LabsLinh Lê
 
Kiểu mảng
Kiểu mảngKiểu mảng
Kiểu mảngLinh Lê
 
LTC-Cấu trúc rẽ nhánh
LTC-Cấu trúc rẽ nhánhLTC-Cấu trúc rẽ nhánh
LTC-Cấu trúc rẽ nhánhLinh Lê
 
Xâu kí tự
Xâu kí tựXâu kí tự
Xâu kí tựLinh Lê
 
Tong quan ve lap trinh
Tong quan ve lap trinhTong quan ve lap trinh
Tong quan ve lap trinhLinh Lê
 
Collections
CollectionsCollections
CollectionsLinh Lê
 
Cấu trúc lặp (loop)
Cấu trúc lặp (loop)Cấu trúc lặp (loop)
Cấu trúc lặp (loop)Linh Lê
 
Hàm (function)
Hàm (function)Hàm (function)
Hàm (function)Linh Lê
 

Mehr von Linh Lê (17)

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

Kürzlich hochgeladen

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Kürzlich hochgeladen (20)

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

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