Packaes & interfaces
Click here to load reader
-
Upload
yugandhar-vadlamudi -
Category
Education
-
view
84 -
download
0
Transcript of Packaes & interfaces
PackagesAccess Specifiers
InterfacesMultiple InheritanceExtending InterfaceInterface vs AbstractPractice Questions
Packages• Packages in Java is a mechanism to encapsulate a group of classes,
interfaces and sub packages.
• Many implementations of Java use a hierarchical file system to manage source and class files.
• example: java.lang
Syntax
Package <package name>
This statement should be used in the beginning of the program
/*defining a package*/
package Demo_Package;public class Demo_Pack{ public void demo_142()
{ System.out.println("hello world"); }}
/*importing a package*/
import Demo_Package.*;public class Demo_1 {
public static void main(String[] y){
Demo_Pack p=new Demo_Pack();p.demo_142();
}}
Access Specifiers
• private: accessible only in the class
• no modifier: so-called “package” access — accessible only in the same package
• protected: accessible (inherited) by subclasses, and accessible by code in same package
• public: accessible anywhere the class is accessible, and inherited by subclasses
Interface• Interface is a pure abstract class
• syntactically similar to classes, but you cannot create instance of an Interface
• Their methods are declared without any body.
• When you create an interface it defines what a class can do without saying anything about how the class will do it
Syntax for declaring interface
• Interface <interface_name>
• {…}
• Syntax for Declaring interfaceInterface <interface_name>
{…}
• Syntax for implementing interfaceClassname implements <interface_name>
{…}
interface Demo_ //interface declaration
{ int int_variable=10;
void demo(); // no method body
}
//implementing interface
public class Demo_interface implements Demo_
{ public void demo() //interface body implemented
{ System.out.println("hellow interface"); }
public static void main(String[] args)
{ Demo_interface d=new Demo_interface();
d.demo();
}
}
Multiple inheritance in javainterface Demo_Interface
{ void Demo_Interface(); }
class Demo
{ void Demo_Print_method()
{ System.out.println("the class Demo method called"); }
}
public class Multiple_Inheritance extends Demo implements Demo_Interface
{ public void Demo_Interface()
{ System.out.println("hello Demo_INterface method called"); }
public static void main(String[] args)
{ Multiple_Inheritance MI=new Multiple_Inheritance();
MI.Demo_Print_method(); //class method called
MI.Demo_Interface(); //interface method called
}
}
Extending interfaceinterface News_Paper_Color
{ void News_Paper_Color(); }
interface Magazine_Paper_Color extends News_Paper_Color
{ void Magazine_Paper_Color(); }
public class Multiple_Inheritance implements Magazine_Paper_Color
{ public void News_Paper_Color()
{ System.out.println("the New Papaer Color red"); }
public void Magazine_Paper_Color()
{ System.out.println("the magazinge paper color yellow"); }
public static void main(String[] args)
{
/*
* we can not create objects for interface class
*/
//News_Paper_Color n=new News_Paper_Color();
Multiple_Inheritance Mi=new Multiple_Inheritance();
Mi.News_Paper_Color();
Mi.Magazine_Paper_Color();
}
}
Abstract Class Interface
Abstract class is a class which contain oneor more abstract methods, which has tobe implemented by its sub classes.
Interface is a Java Object containingmethod declaration but noimplementation. The classes whichimplement the Interfaces must providethe method definition for all themethods.
Abstract class is a Class prefix with anabstract keyword followed by Classdefinition.
Interface is a pure abstract class whichstarts with interface keyword.
Abstract class can also contain concretemethods.
Whereas, Interface contains all abstractmethods and final variable declarations.
Abstract classes are useful in a situationthat Some general methods should beimplemented and specialization behaviorshould be implemented by child classes.
Interfaces are useful in a situation that allproperties should be implemented.