Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Java Khader Sir Notes

Uploaded by

shwetha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
48 views

Java Khader Sir Notes

Uploaded by

shwetha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
(a) Mabech mine shod ey ACADEMY saRAma7T INTERFACES ‘An interface is like a class containing o group of constants and method declarations without their definition or implementation. In other words, an interface allows to specify what a class must do, but not how to do Interfaces are a way to implement multiple inheritance which java does not support directly. ral form of an Defining an interface: An interface is defined like a class. The ge interface is: interface interface-name { return-type method-namel(parameter-list): return-type method-name2(parameter-list); return-type method-nameN(parameter list); Data-type variable 1 = value; Data-type variable 2 = value; Data-type variable N = value; The methods declared in the interface does not have a body. They are abstract methods. The variables declared inside an interface are implicitly “final” and “static’. They cannot be changed by implementing classes, They must be initialized with a constant value, Multiple inheritance through interfaces or Implementing interfaces:- Once an interface has been defined, one or more classes can implement that interface. The general form of a class that includes the "implements" is: class class-name implements interface1, interface2,.... { //class body Scanned with CamScanner oy KVACADEMY 9966648277 } Interfaces are a way to implement multiple inheritance which java does not support directly. Lf a class implements more than one interface the interfaces are separated with a comma. If a class implements more than one interfaces that declare the same method, then there will be only one implementation for both those methods, The type signature of the implementing method must match exactly the type signature specified in the interface definition. The methods that ‘are implemented must be declared public, Example :- _pulyle_ inher Fares lementahion a Base pee 7. . void show: oe } SpyerBacn Bar class Derived implements Base : : & sad dfighy 01 vo; 3 me void show() co Cee tos fu System out println(‘hello show") ) “tae vord shove C) : w ay class Test( “oon (Hells vhos")> public static void main(String args{]) i fais werd Baglys Derived obj= new Derived(); 3 Interfaces:- All the variables declared in the interface are “final” and "static". That is they cannot be changed by implementing class, Also, all the variables must be initialized to some constants. Scanned with CamScanner obj show(); i 0" aCe as us); Sxt0n.. KV ACADEMY 9966648277 Extending Interfaces:- Like classes, one interface can inherit another interface by use of "extends" keyword, The syntax is same as for inheriting classes, interface interface! extends interface2 { // body of the interface } When a class implements an interface that inherits another interface, it must provide implementations for all methods within the interface inheritance chain. ® ;s_impleme! ing interface reference variable we can create areference for aninterface. But we can't create an instance for interface. Interface reference variable can refer to object of class which implements to interface. And also we can access implements by using interface reference variable. Example: interface Base { void public show(); } class Derived implements Base { public void show() { ‘System.out.printIn(‘Hello”); } public static void main(String[} args) { Base obj= new Derived(); //Base test = new Base(); wont compile ‘obj.show(); Scanned with CamScanner } KV ACADEMY 9966648277 INTERFACES Vi 'S CLASSES: vi, Interfaces are abstract classes. Classes can contain method definitions where as interfaces can contain only method declarations, but no definitions, Interfaces can contain only constant variables whereas a class contain any kind of variables. A class can extend only a single class, but it can implement multiple interfaces. Both classes and interfaces provides a way for encapsulation, The interface methods that are implemented must be public whereas the class methods need not be. I ‘ACE VS ABSTRACT CLASS : i. An interface cannot contain any definition of methods . An abstract class can contain definition of some methods. ii, The variables in an abstract class can have any access specifier except private. A variable in an interface can be only public. iii, An abstract class is extended using "extends" whereas an interface is implemented using “implements”. iv, Anabstract class does not support multiple inheritance whereas an interface can have multiple inheritance. PACKAGES: - A packageis a collection of classes and interfaces that provides access protection and name space management. Some of the advantages of packages are : i, ii. iii, iv. The classes contained in the packages can easily be reused. Tava package is used to categorize by the classes and interfaces. It is easy to maintained, Java package is provide as access protection. Scanned with CamScanner wv ACADEMY 9966648277 v. Tt may removes naming collision (you can create more than one class with same name by changing the package.) vi. This packages can be provide reusability of code, Creating a package:- The following are the steps to create a java package. Declare the package at the beginning of a file. Its general form is : package package-name; Where "package" is the keyword. Define the classes and interfaces that is to be kept in the package and declare it public, Tts general form is: package package-name; public class class-name_1 € //body of the class } class class-name_2 { //body of the class class class-name_n ( /Ibody of the class } interface interface-name_t { //body of the interface } interface interface-name_2 Scanned with CamScanner KV ACADEMY 9966648277 y //body of the interface interface interface-name_n { //body of the interface } As per java language specification, there can be only one public class in a file (java) and file name should be same as public class name. If no access modifier is used then it results in default-access. In default access, the class is available to only the members of that package. Create a sub-directory under the directory whose the main source files are stored, Store the program as class-name. java in the subdirectory created above. Compile the program. This creates a *.class" file in the sub-directory. ACCESSING A PACKAGE:- The packages can be accessed by importing them using the "import" keyword. Once the package is imported then the classes in it can be used, The general form of importing a package is: import package-name. class-name; The “package-name " is the name of the package and the “class-name” is the required class in that package. The “class-name" can be replaced by an asterisk(*) which imports all the classes in that package. the “import” statement should appear at the starting of the source file. Multiple “import” statements are allowed. Tf a class with the same name exists in two different packages and if both the Packages are imported then the compiler gives a compile time error. In such a case, when the class is used, it must explicitly named with full packages name. For example : Scanned with CamScanner importjavalang*; kv ACADEMY 9966648277 importjava.util.*; class test{ public static void main(String args{}){ Date d=new Date(); /* gives error because date class is present in both java.lang and jovautil packages. */ } In such case “java.util.Date Example ‘= package N; public class Num * or *java.lang,Date” must be used in place of "date". public fact(int n) { int i, fel: for(iO;ie=nsi++) { fofti; ) return f: } . The above code is the package definition. it must be stored in “N" directory with *Num,java” as file-name. then it must be compiled. It can be used as follows : import N.*; class test { public static void main(String args{]) Scanned with CamScanner KY ACADEMY 9966640277 In the above code, the package "N" is imported. Then the class "Num" cane be used. CLASSPATH:- CLASSPATH is a environmental variable, Lt stores the location of the folders where java compiler searches for classes that are imported through packages. Usually these classes are contained in the "bin" directory of the java folder. For example, it can be like this : CLASSPATH wa\ jdk1.2\bin; When we create our own package, the directory where we store the package must also be included in the CLASSPATH variable. Java Program Structure: A Java program consists of different sections. Some of them are mandatory but some are optional. The Documentation Section: It contain the comments to It improves the readability of the program. Package Statement: It contains a package declaration. Import statements: It contains import statements it provide the facility to access the classes which are created in other packages. Interface Section: It contains interfaces, interface is similar to a class but it contains only abstract methods. Class Section: It contains classes. Every Java program consists of at least one class definition. This class must contains the main method. It is from where the execution of program actually starts. ti. Scanned with CamScanner

You might also like