Lecture Notes Java Software devalopment, Lecture notes of Java Programming

it a complete handout for Java Course and will help you in Java

Typology: Lecture notes

2018/2019

Uploaded on 04/08/2022

muhammad-umer-khan-1
muhammad-umer-khan-1 🇵🇰

5

(1)

1 document

1 / 186

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Interfaces
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Lecture Notes Java Software devalopment and more Lecture notes Java Programming in PDF only on Docsity!

Interfaces

 Using the keyword interface, you can fully abstract a class’ interface from its implementation.

 That is, using interface, you can specify what a class must do, but not how it does it.

 Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.

 To implement an interface, a class must create the complete set of methods defined by the interface.

 However, each class is free to determine the details of its own implementation.

Difference

 You cannot instantiate an interface.

 An interface does not contain any constructors.

 All of the methods in an interface are abstract.

 An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

 An interface is not extended by a class; it is implemented by a class.

 An interface can extend multiple interfaces.

Defining an Interface

 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;

 interface Callback {

 void callback(int param); }

 Once an interface has been defined, one or more classes can implement that interface.

 class classname [extends superclass] [implements interface [,interface...]] {

 // class-body}

 If a class implements more than one interface, the interfaces are separated with a comma.

 The methods that implement an interface must be declared public.

 class Client implements Callback {  // Implement Callback's interface  public void callback(int p) {  System.out.println("callback called with " + p);  }  }  Notice that callback( ) is declared using the public access specifier.

Interface Inheritence

 One interface may inherit another interface.  The inheritance syntax is the same for classes and interfaces.  interface MyInterface1 {  void myMethod1(…) ;  }  interface MyInterface2 extends MyInterface1 {  void myMethod2(…) ;  }  When a class implements an interface that inherits another interface, it must  provide implementations for all methods defined within the interface  inheritance chain.

 When a class implements an interface that inherits another interface, it must

 provide implementations for all inherited methods:

 class MyClass implements MyInterface2 {

 void myMethod1(…) { … }

 void myMethod2(…) { … }

 …

 }

 MyClass must implement all of A and B methods:  class MyClass implements B {  public void meth1() {  System.out.println("Implement meth1().");  }  public void meth2() {  System.out.println("Implement meth2().");  }  public void meth3() {  System.out.println("Implement meth3().");  }  }

 Create a new MyClass object, then invoke all interface methods on it:

 class IFExtend {

 public static void main(String arg[]) {

 MyClass ob = new MyClass();

 ob.meth1();

 ob.meth2();

 ob.meth3();

 }

 }

Why Java is Important

• Two reasons :

  • Trouble with C/C++ language is that they are not

portable and are not platform independent

languages.

  • Emergence of World Wide Web, which demanded

portable programs

• Portability and security necessitated the

invention of Java

History

  • James Gosling - Sun Microsystems
  • Co founder – Vinod Khosla
  • Oak - Java, May 20, 1995, Sun World
  • JDK Evolutions
    • JDK 1.0 (January 23, 1996)
    • JDK 1.1 (February 19, 1997)
    • J2SE 1.2 (December 8, 1998)
    • J2SE 1.3 (May 8, 2000)
    • J2SE 1.4 (February 6, 2002)
    • J2SE 5.0 (September 30, 2004)
    • Java SE 6 (December 11, 2006)
    • Java SE 7 (July 28, 2011)

What is java?

  • A general-purpose object-oriented language.
  • Write Once Run Anywhere (WORA).
  • Designed for easy Web/Internet applications.
  • Widespread acceptance.

How is Java different from C…

  • C Language:
    • Major difference is that C is a structure oriented language and Java is an object oriented language and has mechanism to define classes and objects.
    • Java does not support an explicit pointer type
    • Java does not have preprocessor, so we cant use #define, #include and #ifdef statements.
    • Java does not include structures, unions and enum data types.
    • Java does not include keywords like goto, sizeof and typedef.
    • Java adds labeled break and continue statements.
    • Java adds many features required for object oriented programming.