0% found this document useful (0 votes)
12 views22 pages

Java 2 Unit Notes

The document discusses the concept of interfaces in Java, highlighting their role in achieving abstraction and supporting multiple inheritance. It explains the syntax for defining interfaces, the differences between classes and interfaces, and how to implement interfaces in classes. Additionally, it covers the advantages of using packages in Java for organizing classes and interfaces, along with examples of built-in packages.

Uploaded by

payalsaini26905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views22 pages

Java 2 Unit Notes

The document discusses the concept of interfaces in Java, highlighting their role in achieving abstraction and supporting multiple inheritance. It explains the syntax for defining interfaces, the differences between classes and interfaces, and how to implement interfaces in classes. Additionally, it covers the advantages of using packages in Java for organizing classes and interfaces, along with examples of built-in packages.

Uploaded by

payalsaini26905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
ws me Interfaces _ / ductio! ort multiple inheritance. But, a large numb: t ooh use of multiple inheritance. Therefore ee known as interfaces to support the ci pro: | wt es 0 * tions gers 2 7 , neta in java is also a mechanism to achieve fully abstraction. Interface is He interfac f abstract data members such as methods. All th colection “implicitly public and abstract. The method defined interface rit implementation and only specify the Parameters potnave thel lues they will return. An interface is always imple weype of val lementing an interface must define alll the metho: The class ae Java compiler given an error Message. Interface in Java is 7 nent to an abstract base class. You cannot instantitate a equ iN Object through an terface, but you can offer a set of functionalities that is common to Several different inter ’ classes. er of real-life » Java provides an Oncept of multiple 'e Members of an in an interface do they will take and ‘Mented in a class. ds contained inside What is an Interface? Inleface looks like class but it is not a class, An interface can have methods and Variables just like the class but the methods declared in interface at by default abstract ( (only methed signature, no body). Also, the variables declaredin an: interface ae public, static & final by default. * Interfaces are used for abstraction. Since methods in interfaces does not have body, they have to be implemented by the class before you can access ee set bstinplenis interface must mpl al enna aes FS leva Programming language does not support multiple os Mace We can achieve this as a class can implement more than one interlac —___ ES IS a5 a [Link] [257] L ay public and a a autatical PUDIE NS Roane ey pod and public i: oe eth static and fing)” abstraction” wt the functionality of multiple inheritangs or variables declaration; methods declaration; interface is keyword . \ interfacename is any valid java identifier... \ are, as ds are declared using only their return type and ‘signature. They . We know now that in an interface, no method can have animplemet= vi es declared in the interface definition are constant, therefore, te!® tiaized with a constant value. pysictoc 69 a compiler adds automatically public a, face method and public, oman a) / and fi Wi Ee Note: ‘The jav' pefore the inte! before data members. | s ee "4 Gt me oe nterface e interface. 98 Reasons to-use I re three reasons to ust There al 4. Itis used to achieve fully abstraction. 2. By interface, we can support the functionality of multiple inh | eFitang 2, 3, Itcan be used to achieve loose coupling. 9h Defining an Interface Interfaces are syntactically similar to classes. Syntax is : 3 iE i oh opt /| Interface interfacename ent guasaat en variables declaration; methods declaration; ) Where interface is keyword \ interfé facename is any valid java identifier. “ Methods are de clan methods. We = 7 sing ont tert ype ania. Tee 4 ow that in an interface, no method can have anime 2 All variables d leclared in the it be initialized with a c otatant we definition are are constant, therefore, they value. Soa Sosentis te name of nee, cons es ntai oe 9 40 and one method displ tains one constant num rr nethod display( ). Interface varia cd fil mabe ee Public an eomcan are as shown Interface student int num = void display); } [Link] Interface student tg final int rum = 40; public abstract void display( } 7959] J Aortorences between Class and Interface Differences between class and interface are shown in table 9 1 TABLE 9.1 Class ntertacg Class can have abstract and non- abstract methods. Class doesn't support, multiple inheritace. Class can have final, r Static'and non-static variables. It Gan use various access specifiers like public, or protected. A class implements the interface. Class can have static methods, main method and constructor. n-final, It can be instantiated by declaring objects. objects. It can - 4 ee Only by