Java ProgrammingObject oriented programming with java

Object oriented programming with java

In this tutorial we will learn the various object oriented programming methods with java and their implementations like interfaces, constructors, this keyword, super keyword,etc…

  • To get brief details about object oriented programming with java just go through the following steps below :

A.Creating a java type with classes and interfaces, constructors, this keyword, super keyword : –

  1. Class : – Classes is a blueprint, templates. Classes are used to create object. Class is define the keyword class. Class name same as dot java file name. It is case sensitive. Java allows you to define user-defined classes.
  2. Interfaces : – Java does not support multiple inheritance so we can use interface. Its concept is almost like abstract class. In interface you can declare a method, but you cannot define a method. You cannot create object of interface. You can simply use keyword interface. Interface provide you security.
  3. Example :

    public class TypeOfVegy implements Vegetable//create class and implements interface
    {	
    	public void green()//method 
    	{
    		System.out.println ("Vegetables are green"); //print statement
    	}
    	public void unhygienic()//method 2
    	{
    		System.out.println ("Vegetables are unhygienic");
    	}
    	public void healthy()// method 3
    	{
    		System.out.println ("Vegetables are always healthy");
    	}
    	public static void main(String[] args)//main method
    	{
    		TypeOfVegy tof = new TypeOfVegy (); //create class object
    		tof.green (); //call method 1
    		tof.unhygienic (); //call method 2
    		tof.healthy (); //call method 3
    	}
    }
    //save interface as Vegetable
    interface Vegetable //create interface
    {
    	public void green();//define method 1
    	public void unhygienic();//define method 2
    	public void healthy();//define method 3
    }
    

    Output :

    Vegetables are green
    Vegetables are unhygienic
    Vegetables are always healthy
    

  4. Constructor : – Constructor name is same name as class name, it has no return type. System have in-built feature of constructor, if you do not define constructor, then the compiler will creates a default constructor implicitly. Multiple constructors we can provide with different signatures. When you create any object constructor called automatically. There is two types of constructors in java: default constructor which do not contain any parameter and it is created if and only if there is no constructor defined and parameterized constructor which contains parameters to pass parameters on creation of objects. To create objects for a class constructors are required.
  5. Example :

    public class DefaultConstructor //class
    {
    	public DefaultConstructor ()//default constructor
    	{
    	System.out.println ("This is Default Constructor.!"); //print statement
    	}
    	public static void main (String[] args)//main
    	{
    	DefaultConstructor dc = new DefaultConstructor (); //create object of class
    	}
    }
    

    Output :

    This is Default Constructor.!

    Example :

    public class ParameterConstructor //create class
    {
    	private String name; // define String
    	public ParameterConstructor(String str) // parameterized constructor
    	{
    		this.name = str; //this used for reference
    System.out.println ("This is Parameterized Constructor.!"); //print statement
    		System.out.println ("The value is:"+str); //print value
    	}
    	public static void main(String[] args)//main method
    	{
    	ParameterConstructor pc = new ParameterConstructor ("Chingi");
    //create object of class 
    	}
    }
    

    Output :

    This is Parameterized Constructor.!
        	    The value is: Chingi
    

  6. This : – this keyword we can used in constructor level as well as field level. this keyword we can used for reference. This keyword we must write in the constructor’s first line. This this keyword is used for call constructor of the same class in java and used to call overloaded constructor. We can call methods of class by using this. this keyword also used to return object. It is a non-static and cannot be used in static context, it means we cannot use this keyword inside main method in java.
  7. Example :

    package This;
    
    class Employee
    {  
        int Eid;  //define integer
        String Ename; //define string
          
        Employee (int Eid, String Ename)//parameterized constructor
        {  
        this.Eid = Eid; //this is used for solve ambiguity between instance variable //and parameter.
        this.Ename = Ename; //this is used for solve ambiguity between instance //variable and parameter.
        }  
        void show()// declare method
        {System.out.println (Eid+" "+Ename+" ") ;} //print employee detail 
        public static void main(String args[])//main method
        {   Employee e1 = new Employee (101,"Pritam"); //create object and put values 
        	Employee e2 = new Employee (102,"Priyan"); //create object and put values 
        	e1.show (); //call method
        	e2.show (); //call method}} 
    

    Output :

    101 Pritam
    102 Priyan

  8. Super : – It can be used in method as well as constructor. It is used to call super class constructor. It is non-static and cannot be used in static context, it means we cannot use super keyword inside main method in java. It is used to store the super class object into sub class.
  9. Example :

    //save as Fruit
    package abc;
    
    class Fruit {
    	String colour = "yellow";
    }
    
    //save as Banana
    package abc;
    
    class Banana extends Fruit //extends class fruit
    {
    	String banana; //define string
    	void show () //declare method
    	{
    		System.out.println (super.colour);//will print colour of fruit now  
    	}
    	public static void main (String [] args) //main method 
    	{
    		Banana a = new Banana (); //create object of class
    		a.show (); //call method	
    	}
    }
    

    Output :

    yellow

B.Inner Classes and Closures : –

  1. Inner Classes : – Inner class is that a one class define inside another class. Two categories of inner class is that static inner class and non-static inner class. Static inner class is called as nested class. Any class which is not declared inside another class is known as nested class. And non-static inner class is called as inner class. We also use inner classes to implement helper class. In static inner class cannot have instances, but in non-static inner class can have instances which belong to the outer class. Nested class help in maintenance and also improves the encapsulation.
    Two additional types of inner classes. Within the body of a method you can declare an inner class these classes are known as local classes. Local classes are defined in a block, which contain group of zero or more statements with balanced braces. It has access to local variable. It can only access local variables that are declared final. This classes is similar to inner classes because they cannot declare or define any static member. We can also declare an inner class within the body of a method without naming the class, this class is called as anonymous class. This is helps you to make your code more short. Anonymous classes are expressions, it means you define the class in another expression. It has access to the members of its enclosing class. You cannot declare it as final, so it cannot access local variables in its enclosing scope. Static initializers or member interfaces you cannot declare in an anonymous class. Fields, extra methods, instance initializers and local variables are declare in anonymous classes. Constructors are not allowed in anonymous class.
    Four types of inner classes: –
  2. a.Static Member Classes : –It has access to all static members of its containing class, including private member also. It can be declared with its own access control modifiers. It cannot have the same name as any of its enclosing classes.

    b.Member Classes : –This is a member of a class.

    c.Local Classes : –This is defined within a block.

    d.Anonymous Classes : –It has no name.

    Example :

    package InnerClass;
    
    public class InnerClassExample
     {
        public static void main(String args[]) 
    {
                  //creating local inner class inside method
            class LocalInner {
                public void show() {
                    System.out.println ("This is a Local inner class..!");
                    }    }
          
            //creating instance of local inner class
            LocalInner loc = new LocalInner ();
            loc.show (); //calling method from local inner class
          
            //Creating anonymous inner class in java for implementing thread
            Thread anonymous = new Thread () {
                public void run(){
                   System.out.println ("This is an Anonymous inner class..!");
    }
    };
            anonymous.start ();//call method
          
            //example of creating instance of inner class
            InnerClassExample ine = new InnerClassExample ();
            InnerClass ic = ine.new InnerClass ();
            ic.display (); //calling method of inner class
        }   
         //Creating Inner class in Java
            private class InnerClass
    {
            public void display()
    {
                System.out.println ("This is an Inner class..!");
           } 
    } 
    }
    

    Output :

    This is a Local inner class..!
    This is an Anonymous inner class..!
    This is an Inner class..!
    

Thus, we had a brief lookout over object oriented programming with java.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -