OOPS PRINCIPLES

OOPS PRINCIPLES IN JAVA

The Object Oriented Programming principles in Java Language are

1) CLASS
2) OBJECT
3) ENCAPSULATION
4) INHERITANCE
5) POLYMORPHISM

CLASS
  • A class consists of variables and methods.
  • The combination of variables andmethods are known as members of the class.
Syntax :

           class  classname
          {
             type instancevar1;
                ……
             type instancevarN;

             type methodname1()
            {
               // body
            }
               ……
            type methodnameN()
           {
              //body
           }
         }

NOTE:-

  • In the class, the variable is known as instance variable because when we create object for a class a copy of variable is stored in the object and in java object is also known as instance.

OBJECT
  • In java, we can create object for a class by using new operator dynamically at run time.
Syntax:

          classname  objectname = new classname();
                                          or
          classname objectname;//here objectname is variable and null is stored
          objectname = new classname();

ENCAPSULATION
  • It binds together code and data from outside misuse.
NOTE:-
  • In a java program we use private keyword for a variable to achieve encapsulation.
INHERITANCE

Definition 1: Inheriting all the properties of one class to another class is known as inheritance.

Definition 2: Inheriting all the properties of super class by sub class by using the keyword extends is known as inheritance.

NOTE:-
  • The class which is inherited is known as superclass.
  • The class that does inheriting is known as subclass.
TYPES OF INHERITANCE

1) Single level Inheritance
2) Multilevel Inheritance
3) Multiple level Inheritance
4) Hierarchical Inheritance.
  • In single level inheritance there will be only two classes.They are one super class and one sub class.

                      A (Super class)

                      B (Sub class)

  • In a multi-level inheritance, a sub class can be a super class for another sub class.

                     A (Super class)

                     B (Sub class)

                     (Super class)

                     C (Sub class)

                     (Super class)

                     D (Sub class)


  • In multiple level inheritance, a sub class can inherit two or more super classes at a time.

                     A B C …….(Super class)

                                 D (Sub class)

  • Hierarchical Inheritance is a combination of multi and multiple level inheritance.
NOTE:-
  • Multiple level inheritance is not supported by Java.
  • To achieve multiple level inheritance in Java we have to use interface.
INTERFACE
  • Interface consists of methods declaration.
  • The methods declared in the interface must be public abstract void methods.
  • The methods declared in the interface can be implemented in a class as public void methods.
  • The methods declared in the interface must compulsorily be implemented in a class.
  • Interfaces are implemented by a class by a using the keyword implements.
  • One interface can inherit another interface by using the keyword extends.
  • Interface resembles to polymorphism i.e., one interface and multiple methods.
  • By default the methods declared in the interface are abstract methods.

STEPS TO WRITE JAVA PROGRAM
  • There are six steps to write a Java Program. They are:

1) DOCUMENTATION SECTION
2) PACKAGE STATEMENT
3) IMPORT STATEMENT
4) INTERFACE STATEMENT
5) CLASS DEFINITION
6) MAIN METHOD

1) DOCUMENTATION SECTION
  • Here we have to write comment.
  • Non executable code in the program is known as comment.
In Java there are 3 types of comments.They are:

1. Single line comment:
  • In single line comment, the comment is written in single line.

       Symbol: //

2. Multi line comment:
  • In multi-line comment, multiple lines are written.
     Symbol: 
                  /*
                  ………………
                  ……………….
                  ………………
                 */

3. Documentation comment
  • This comment is used to prepare the document.
    Symbol: 
                 /**
                 * ………………
                 *……………….
                 * ………………
                 */
2) PACKAGE STATMENT
  • Package is a group of classes, sub classes and interfaces.
  • Package resembles to directory structure.
Syntax:-
             package     packagename;

3) IMPORT STATEMENT
  • By using this import keyword we can import a package into a file.
Syntax:-
                     import packagename.*;
                     import packagename.class name;
                     import packagename.interfacename;

6) MAIN METHOD

Syntax:-
               public static void main(String args[])

EXPLANATION

              public   - keyword
              static     - keyword
              void      - keyword
              main     - method
              String    - built in class present in java.lang package
              args       - arguments
              []           - array