Articles by "Features"
Showing posts with label Features. Show all posts
Java Notes For Java Beginner
Abstract And Final Class

Abstract class

  • A partially implemented and partially unimplemented class, is called as abstract class.
  • Definition : A class which have at least one abstract method, is called as abstract class explicitly.
  • Keyword abstract is used to declare this class explicitly, Abstract class cannot be instantiated because it may have abstract method.

Write a program to define abstract class execution.
abstract class Abs1
{
 public void fun1()
 {
  System.out.println("Hello abstract class method");
 }
 abstract public void fun2();
}

class Abs extends Abs1
{
 public void fun2()
 {
  System.out.println("Hello fun2 sub class method");
 }
 
 public static void main(String args[])
 {
  Abs a1 = new Abs();
   a1.fun1();
   a1.fun2();
  Abs1 a2 = new Abs();
   a1.fun1();
   a1.fun2();
 }
}



Abstract class can created in 3 ways:
1) Declare abstract method in class.
2) Declare keyword 'abstract' with class.
3) Do not implement the interface completely.

Final class:

  • A class which can Inherit others but cannot be inherited by other is called final class.
  • Keyword "final" is used to declare final class explicitly.
  • final class is the last class of inheritance chain.
Write a program implement final class execution.
final class fin extends object
{
 public void fun1()
 {
  System.out.println("Hello final class method");
 }
}
class fun1
{
 public void fun2()
 {
  System.out.println("Hello sub class method");
 }
 public static void main(String args[])
 {
  fin1 f1=new fin1();
  //f1.fun1();
  f1.fun2();
  fun f2=new fun();
  f2.fun();
 }
}


Difference between abstract and final class.
1) Abstract class cannot be instantiated.
2) Abstract class should be inherited in some other class.
3) Abstract class may have abstract methods.
4) Super class reference and sub class object concept is followed by abstract.
5) Abstract keyword is used to declare this class
1) The object creation process of final class is compulsory.
2) final class cannot be inherited.
3) final class doesnot have abstract method.
4) this concept is not followed by final class.

5) final keyword is used to declare this class.


Note : The class can be either abstract or final.


abstract
final
static
  • class
  • variables
  • methods
  • constructor
Java Notes For Java Beginner

Inheritance:

The concept of reutilazation is called as inheritance.
Inheritance is used to eliminate the reduandancy of program.


Note : Keyword "extends" is used to inherit one class into another class.

The class which gives it's properties to another class is called as super class or base class.
The class which takes properties from other class is called as sub-class or derived class.

Syntax:
class B extends A
{
-----
-----
}

Types of inheritance

1] Single inheritance :

The one to one communication between super and sub class, this concept is called as single inheritance.

/*Example of simple inheritance */
class A
{
 void fun1()
 {
  System.out.println("Hello this is Super class method");
 }
}
class B extends A
{
 void fun2()
 {
  System.out.println("Hello this is Sub class method");
 }
 
 public static void main(String args[])
 {
  A a1=new A();
  B b1=new B();
  
  a1.fun1();
  b1.fun2();
  b1.fun1();
 }
}


2] Multilevel inheritance :

To inherit more than one classes in chain format with a finite termination, this concept is called as multilevel inheritance.

Tip [Rule] message : One java class can extends only one java class.
According to this rule, Java doesn't follow multiple, Multi-path and Hybrid inheritance.

Note : Class object is the super must built in class of java hierarchy. Every built in and user defined classes should inherit the object class. This class provides the context creation, constructor calling, data overloading and execution properties to the object creation statement.


JAVA Does not Follow The Cyclic Inheritance.

Constructor are executed in the manner they are defined in inheritance


Write a program to define the constructor execution inheritance.

class A extends object
{
 A()
 {
  System.out.println("Class A Constructor");
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B Constructor");
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C Constructor");
 }
}
class D extends C
{
 D()
 {
  System.out.println("Class D Constructor");
 }
 
 public static void main(String args[])
 {
 D d1=new D();
 C c1=new C();
 B b1=new B();
 A a1=new A();
 }
}



Super


Note : Kayword 'super' is used to call the super class parameterized constructor from sub class constructor.

"Super" should be the first statement of constructor body.

Write a program to call the super class default and parameterized constructor from sub class.

class A
{
 A()
 {
  System.out.println("Class A constructor");
 }
 A(int a)
 {
  System.out.println("A is "+a);
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B constructor");
 }
 B(int b)
 {
  System.out.println("B is "+b);
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C constructor");
 }
 C(int c)
 {
  System.out.println("C is "+c);
 }
 
 public static void main(String args[])
 {
 C a1=new C();
 }
}


Java program execution criteria :


JVM loads all the static data of class into memory. At last it loads the main method. But executes the main() method first to all main() method creates objects.
Object's call the constructor creates context for remaining non-static data of class. It loads this data into newly created context and assign the control to the object.

Method overriding :


To define the same name method with same signature and same arguments in super class as well as in sub class, this concept is called as method overloading.

Note : Keyword "super" is used to call the super class overrided method from sub class method.

Write a program to define methods overriding.

class P
{
 void fun1()
 {
  System.out.println("Hello super class method");
 }
}
class Q extends P
{
 void fun1()
 {
  Super.fun1();
  System.out.println("Hello Sub class method");
 }
 public static void main(String args[])
 {
  Q q1=new Q();
  q1.fun1();
 }
}
Java Notes For Java Beginner
A special type of method, which is used to construct memory for class elements, is called as constructor.

Example :
class A
{
 A()
  {
  }
}


It's specialties are

  • It's name and class name is same.
  • It executes implicitly on object creation statements.
  • Constructors cannot be executed explicitly by objects that one constructor in it's life time.
  • Constructor doesn't have any return type not even void


Example :
class AB
{
 AB()
 {
  System.out.println("Hello Constructor");
 }
 public static void main(String args[])
 {
  AB a1=new AB();
  AB a2=new AB();
  AB a3=new AB();
  AB a4=new AB();
  }
}


Types of Constructor :

There are two types of constructor in Java.

1.Default Constructor :

A constructor which does not accept arguments, is called the default constructor.

Parameterized Constructor :

A constructor which have parameters and accepts arguments at runtime is called as parameterised constructor.


Write a program to define constructors overloading.
class ABC
{
 ABC()
 {
  System.out.println("Default constructor");
 }
 ABC(int x)
 {
  System.out.println("X is "+x);
 }
 ABC(float p)
 {
  System.out.println("P is "+p);
 }
 ABC(int a,int b)
 {
  System.out.println("A is "+a+" B is "+b);
 }

public static void main(String args[])
 {
 ABC a1= new ABC();
 ABC a2= new ABC(100);
 ABC a3= new ABC(12.5);
 ABC a4= new ABC(1,10);
 }
}


Construction execution criteria[Process] :

There are four steps in execution criteria of Constructor.
  1. TO create context.
  2. To load the class data into that context.
  3. To initialize all the uninitialized data of context.
  4. To execute it's own body.

Java Notes For Java Beginner

Structure oriented language 'C' when implemented with new object oriented concepts, it becomes 'C++'.
These new language doesn't support/follow the concepts so C++ is also known as partial object oriented language. Both above languages are 16 bits (DOS-Base) application (Window based), implements all oops (object oriented programming) concept by removing the structural orientation from it. This new language is called 'OAK' It is Designed & Developed by the team whose leader is 'JAMES GOSLING' at 'SUN MicroSystems' USA in 1991.

Preliminary, this language is used for repairing & manufacturing of electronic equipment's like radio, transistor, press, etc... As this language follows better network, so the development team uses this application for chatting in Intranet process. This team creates a web browser for mailing of large data & named as 'Hot-JAVA'.

Later on, it comes into consideration, that this language is also applicable for academic purpose, So it again lunch at 1995 for the same purpose & Just renamed it as 'JAVA'. Java is famous for its slogan "Write Once, Run Anywhere."

There were five primary goals in the creation of the Java language:
  • It must be "simple, object-oriented, and familiar".
  • It must be "robust and secure".
  • It must be "architecture-neutral and portable".
  • It must execute with "high performance".
  • It must be "interpreted, threaded, and dynamic".

Java Versions:

  • JDK 1.0
  • JDK 1.1
  • J2SE 1.2
  • J2SE 1.3
  • J2SE 1.4
  • J2SE 5.0
  • Java SE 6
  • Java SE 7
  • Java SE 8 
Read More About Java version history here.

Features of JAVA

  • Java is secured language.
  • Java is complied and interpreted language.
  • Java is platform independent language [It means we can compile sthe java application on one machine and run it on other machine with same or other platform (operating system). ] 
  • Java is famous for its slogan "WORA" means "Write Ones, Run Anywhere".
  • Java is Simple, Robust & Dynamic language.
  • Java is multi-threaded & distributed language.
  • 'javac' command is used to invoke the .java file to it. compiler converts it into it's equivalent .class file (Byte code).
  • 'Java' command is used to call the java interpreter (JVM-JAVA Virtual Machine), assign th .class file without extension to this command. It converts it into it's .exe file, print's the output & delete the .exe file from memory.
  • Program should be save with the class name in which main() method is defined.
  • Example: File Name: A.java
    class A
    {
    public static void main(String args[])
    {
    System.out.println("Hello");
    System.out.println("How Are You ? ");
    System.out.println("Bye... ");
    }
    }
    
  • Context Creation : Context means the memory created by object for the class elements.
  • Example: File Name: B.java
    class B
    {
    int x=10;
    int y;
    void fun1()
    {
    System.out.println("Hello Fun1");
    }
    public static void main(String args[])
    {
    B b1=new B();
    b1.fun1();
    b1.fun1();
    System.out.println(b1.x);
    System.out.println(b1.y);
    b1.y=50;
    System.out.println(b1.y);
    B b2=new B();
    System.out.println(b2.x);
    System.out.println(b2.y);
    b2.fun1();
    }
    }