Sunday 23 October 2016

abstract class

abstract class

  • We can create abstract class by using abstract keyword.
  • A class which is declared as abstract is known as abstract class.
  • abstract class can contains,
    • constructor
    • variables
    • abstract methods
    • non abstract methods
    • sub class
  • abstract methods will be implemented in sub class of abstract class.
  • If sub class didn't provide the implementation for abstract method then we need to declare that sub class as abstract class.
  • If any class extends this sub class then that sub class will provide the implementation for abstract methods.
  • object creation is not possible for abstract class.
Syntax
                abstract class NameOfTheAbstractClass
                { 
                      It can contains variables, abstract or non abstract methods; 
                }
Fun point
Nagi :  Hari, in abstract class we learn implemented and unimplemented methods, 
    • Do we have concepts like, 
    • implemented and  unimplemented  variables?
    • implemented and  unimplemented  constructors ?
    • implemented and unimplemented  blocks ?
Hari :  See Nagi, up-to now there is no such type of topics in Java.
    • Both we will sit and develop these things and ll the keep name as Bisket coding language. 
--------------------------------------------------------------------------
Program         :       abstract class 
Program name  :       Test1.java
Output            :         
 This is method1
 This is method2

abstract class Demo1
{
public void method1()
{
 System.out.println("This is method1");
}
public abstract void method2();
}

class Demo2 extends Demo1
{
public void method2()
{
 System.out.println("This is method2");
}
}

class Test1
{
     public static void main(String args[])
     {
           Demo2 demo2 = new Demo2();
           demo2.method1();
           demo2.method2();
     }
}

Compile      :          javac Test1.java
Run            :          java Test1
Output        :         
This is method1
This is method2
--------------------------------------------------------------------------
Program         :       abstract class 
Program name  :       Test2.java
Output            :
                             This is method1
                             This is method2
                             This is method3        

abstract class Demo1
{
public void method1()
{
System.out.println("This is method1");
}
public abstract void method2();
public abstract void method3();
}

abstract class Demo2 extends Demo1
{
public void method2()
{
System.out.println("This is method2");

}
}

class Demo3 extends Demo2
{
public void method3()
{
System.out.println("This is method3");
}
}

class Test2
{
public static void main(String args[])
{
Demo3 demo3 = new Demo3();
demo3.method1();
demo3.method2();
demo3.method3();
}
}
Compile      :          javac Test2.java
Run            :          java Test2
Output        :         
                             This is method1
                             This is method2
                             This is method3
--------------------------------------------------------------------------
Thanks for your time.
Nireekshan