Object oriented programming with java

0
2286

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 :


    Output :

  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 :


    Output :


    Example :


    Output :

  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 :


    Output :

  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 :


    Output :

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 :


    Output :

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here