Packaes & interfaces

10

Click here to load reader

Transcript of Packaes & interfaces

Page 1: Packaes & interfaces

PackagesAccess Specifiers

InterfacesMultiple InheritanceExtending InterfaceInterface vs AbstractPractice Questions

Page 2: Packaes & interfaces

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

Page 3: Packaes & interfaces

/*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();

}}

Page 4: Packaes & interfaces

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

Page 5: Packaes & interfaces

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>

• {…}

Page 6: Packaes & interfaces

• Syntax for Declaring interfaceInterface <interface_name>

{…}

• Syntax for implementing interfaceClassname implements <interface_name>

{…}

Page 7: Packaes & interfaces

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();

}

}

Page 8: Packaes & interfaces

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

}

}

Page 9: Packaes & interfaces

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();

}

}

Page 10: Packaes & interfaces

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.