interface inner classes anonymous classes packages


Interface
Ø  Interface is a pure abstract class,Consists of only public abstract methods, and public static final variables .
Ø  which is represents with “ interface ” keyword.
Ø  Interface provides the blue print for the subclasses.
Ø  eventhough if we given “void m1()” ,it automatically will takes “public abstract m1()”;
Ø  If we declare a variable “int x=2;”it automatically takes “public static final int x=2”,
Ø  The subclass uses “ implements ” keyword to override the methods of interface.
Ø  Interface can also supports all five Inheritance principles including multiple inh.
Ø  We can also use “ extends ” keyword between two interfaces.
Ø  A class can also implements multiple interfaces at a time.
Note:-If any sub class is unable to give definition for the interface methods, then  that class must be declared as Abstract class.
  
  extends

implements
   
Class   to  Class

Interface  to Class
   
Interface  to Interface
  
Syntax:---
interface     <interfacename>
{
public static final variables;
public abstract methods.
}
Ex1:---
interface AB{
int x=8;
void sum();
public abstract void mm();
}
class Sub implements AB{
int y=7;
public void sum(){
System.out.println(x);
}
public void mm(){
System.out.println(“I am in mm() method”);
}
public static void main(String [] args){
Sub   s = new   Sub();
s.sum();
s.mm();
}  }
Ex 2: -
interface Stud
  {
void getName();
void  getRno();
}
interface Marks extends Stud
 {
  void get course();
 void getMarks();
}
Class Sub implements Stud,Marks
 {
Code
}

Summary of Interfaces

An interface defines a protocol of communication between two objects.
An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
A class that implements an interface must implement all the methods declared in the interface.

Differences between Abstract class and Interfaces
             Abstract Class

1.Abstract methods are usually declared where two
or more subclasses are expected to fulfill a similar
role in different ways through different
implementations

                          Interfaces

1.Interface is a pure abstract class,
Consists of only public abstract methods,no concrete methods.

If we don’t give public it gives error.

2. Contains Abstract keyword.

2. Contains Interface keyword.
3.can contain normal variables also
3.contains public static final variables only,can’t change the value once we assign,gives error

The final Keyword in Java

In Java, final keyword is applied in various context. The final keyword is a modifier means the final class can't be extended, a variable can't be modified, and also a method can't be override.
1. Final variables                             
If any variable declaration consists of  “final”  keyword ,we can call it as Final variable.
if a variable is final it works as constant it cannot be changed. in java Final variables are represented with capital letters.
private final int INCREMENT = 5;
Note:- final variables must be initialized othrwise gives error.
 final int I=4;//valid
static final int MIN=3;//this we can access directly.
Note: -final variables are also declared as  static and non-static.
Ex 1:--
public class varconstltr{
   final int i=10;
  public static final int constint=5;
  public static void main(String[] args) {
System.out.println(new varconstltr().i);
  } }

2. Final Methods

1. Final method can’t override in derived classes becos it is final so Can‘t override.
2. If any method prototype consists of ‘final” Keyword  we  can  call it as Final Method.
3. The Fianl Methods can also represent as static and non-static.
public class FinalMethod {
  public final void finalMethodClass()  {
        System.out.println("This is final method of java");
  }
}
public class SubClassFinalMethod extends FinalMethod{
      public void finalMethodClass(){
                  // Error by compiler     // Cannot override the final method from FinalMethod         }
3 . Final Classes
1. If any class declarations consists of “final” Keywords we can call it as Final classes.
2. Final classes can’t inherited to other classes.
public final class FinalClass{
}
/*public class SubClass extends FinalClass{
      public static void main(String[] args) {
//compilation error
      }
}*/
Inner Classes
ü  We can also create or define one class inside another class. It is known as inner class technology.
ü  We can create any no of inner classes inside another class.
ü  We can declare only non-static members in innerclasses.
ü  Inner classes don’t allow STATIC members in the declaration.
ü  Inner classes can also follows INHERITANCE relations.
ü  It is a way of logically grouping classes that are only used in one place.
ü  It increases encapsulation. Nested classes can lead to more readable and maintainable code.
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.
An Instance of InnerClass Exists Within an Instance of OuterClass
class OuterClass {
    ...
    class InnerClass {
        ...
    }
}
Ex:--
class Outter{
 class Inner {
       void sum() {
                   System.out.println("sum from inner class" );
                   }
 }
 class I2 extends Inner {
       void add(){
                   System.out.println("add from inner2 method");
 }  }
 void m1(){
       I2  ii = new  I2();
       ii.sum();
       ii.add();
      }
public static void main(String [] args){
      Outter    obj  =  new   Outter ();
      obj.m1();
      }
}
  Anonymous class or nameless Class
interface Wish
{
void  hello();
}
class AnonyDemo  implements Wish
{
Public static void main(String [] args)
{
Wish   w =new  Wish(){//anonymous class block
public   void   hello(){
System.out.println(“  welcome ”);
}
};//anonymous class close
w.hello();
}

Packages
1. Package is a java folder which is used to group the classes,interfaces,enums..etc.
2. The packages are categorized into two types.                                       
          pre-defined                                                                    
                    User defined
1Provided by SUN-MICRO Systems
Ex: java.lang, java.io ,java.math ,java.util…….etc     
1. Created by USER.
2.Userdefined packages are created by the keyword                                                           
“package”.

   Syntax:--
package    <packagename>;
package   p1;
Note: package statement must be the first statement in program.
Ex:--
package  p1;
import java.lang.*;
class  Abc{
      public static void main(String[] args) {
                  System.out.println("This is package ex");
      }
}
Execution:---
D:\core\>javac –d   d:\core  Abc.java
> java  p1.abc
 Packages contain a set of classes in order to ensure  that its class names are unique.
Packages are containers for classes that are used  to compartmentalize the class name space.
“ –d “------creates one directory p1.
“ d:\core ” -------In the current directory.
Abc.java---filename
         When we are generaring, the compiler will generate one .class file and that .class file is placed In the newly created package or folder.
                        D:
                             Core
                                           P1
                                                   Abc.class
Note:-  If any class called through its package then it is known  as fully qualified name.
Creating sub-packages
   We can create the sub-packages as like we create one folder inside another folder.
      Folder Hierarchy
                  P1
                         P2
                               P3
Ex:--
package p1.p4;
class Test   {
public static void main(String [] args)  {
  System.out.println("hello");
     }    }
Compilation & Execution
>Javac –d . Test.java
>Java p1.p4.Test
Importing one package members into another package
   We can also import one package members into another package externally by using the import keyword.
package calc;
 public class Test1  {
public int sum(int x,int y) {
      return (x+y);
     }
public static void main(String [] args)  {
Test1 t= new Test1();
int s=t.sum(3,4);
System.out.println("The sum is:"+s);
} }
package  p2;
import calc.*;
  class Test2  {
public static void main(String [] args) {
Test1 t1= new Test1();
int ss=t1.sum(14,15);
System.out.println("The sum is:"+ss);
} }
Execution
Javac –d . Test1.java
Java   calc.Test1
Javac –d  . Test2.java
Java   p2.Test2

No comments:

Post a Comment