DEV Community

Cover image for Java OOP Concepts
Mahich123
Mahich123

Posted on

Java OOP Concepts

Class in Java

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.

//Creating Student class.  
class Student{  
 int id;  
 String name;  
}
Enter fullscreen mode Exit fullscreen mode

Static in Java

Static is a keyword. If this keyword is prefixed before the function name, the function is static function. If a method / function is static than it can be access from anywhere.

But if method is not static than it can't be run or accessible. Like below:

public static void main(String[] args) { 
}
Enter fullscreen mode Exit fullscreen mode

here main method is static so it is static method
but in this bellow method :

void calling() { 
}
Enter fullscreen mode Exit fullscreen mode

this one is not static it's a non-static method. So we can't run it. If we want to run a program we have to run through main method and the main method is always static if we remove static from main method than our main method won't run. And as it is static we can't call a non-static method inside a static method like bellow:

public static void main(String[] args) { 
      calling();
}
Enter fullscreen mode Exit fullscreen mode

But we can call or access a static method inside a non-static method like bellow:

static void fun() {

}
void calling() { 
  fun();
}
Enter fullscreen mode Exit fullscreen mode

SO there's a way of calling it inside static method.
By creating object. So we have to create object than we can access no-static method through object like bellow:

public static void main(String[] args) { 
    Main obj = new Main(); // so here i'm creating object for my class .
    obj.calling();        // Here calling it through the object.
} 

void calling() { 

}
Enter fullscreen mode Exit fullscreen mode

So this way we can use non-static method or function.


Function/method Overloading

If I have multiple function/method with same name then it's called method/function overloading , and it should have different arguments a b d.... or type of arguments, and should be different means int or string .... Like bellow:

static void fun(int a) {
      System.out.println();
  }

    static void fun(String a) {
        System.out.println();        
  }
Enter fullscreen mode Exit fullscreen mode

Constructors

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. Like below:

Public class MyClass {
   Int num;
   MyClass() {
      num = 100;
   }
}

public class ConsDemo {
   public static void main(String args[]) {
      MyClass t1 = new MyClass();
      MyClass t2 = new MyClass();
      System.out.println(t1.num + " " + t2.num);
   }
}
Enter fullscreen mode Exit fullscreen mode

Inheritance

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

class Employee{  
 float salary=40000;  
} 

class Programmer extends Employee{  
 int bonus=10000;  
 public static void main(String args[]){  
   Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
   }  
}
Enter fullscreen mode Exit fullscreen mode

Here for accessing the value of Employee class we have to use extends keyword in main class or destination class


Types of Inheritance

  • Single Inheritance :

One class extends another class

  • Multilevel Inheritance:

One class can inherit from derived class and derived class will become parent for another new class

  • Multiple Inheritance :

Once class extends more than one class then it's called multiple inheritance. Java does not support multiple inheritance. We can do this in interfaces.

  • Hierarchical Inheritance:

One class is inherited by multiple classes.

  • Hybrid Inheritance:

Combination of single and multiple inheritance. Java don't have hybrid inheritance.


Polymorphism

Polymorphism means many ways to represent single entity or item.
Types of Polymorphism :

  • Compiled time or static.
  • Runtime or Dynamic.

Overriding

Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

  • Static methods can't override
    Because static method will be called and it doesn't depends on objects so it can't be override

  • Overriding depends on objects.


Instance Variable

  • The variables that are declared inside the class but outside the scope of any method are called instance variables in Java.

  • The instance variable is initialized at the time of the class loading or when an object of the class is created.

  • An instance variable can be declared using different access modifiers available in Java like default, private, public, and protected.

  • Overloading and Overriding doesn't applies to instance variables.

image


Encapsulation

Encapsulation is a process of wrapping code and data together into a single unit.

Advantage of Encapsulation in Java

  • By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.

  • It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.

//A Java class which is a fully encapsulated class.  
//It has a private data member and getter and setter methods.  
package com.javatpoint;  
public class Student{  
//private data member  
private String name;  
//getter method for name  
public String getName(){  
return name;  
}  
//setter method for name  
public void setName(String name){  
this.name=name  
}  
}
Enter fullscreen mode Exit fullscreen mode
//A Java class to test the encapsulated class.  
package com.javatpoint;  
class Test{  
public static void main(String[] args){  
//creating instance of the encapsulated class  
Student s=new Student();  
//setting value in the name member  
s.setName("hi");  
//getting value of the name member  
System.out.println(s.getName());  
}  
}
Enter fullscreen mode Exit fullscreen mode

Abstraction

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body.


abstract class Animal {
  public abstract void animalSound();
  public void sleep() {
    System.out.println("Zzz");
  }
}
Enter fullscreen mode Exit fullscreen mode

Private

The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.

If we make any class constructor private, we cannot create the instance of that class from outside the class.


Public

The public keyword is an access modifier used for classes, attributes, methods and constructors, making them accessible by any other class.


Protected

The protected keyword is an access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses.


Rules of modifiers

Image

Here '+' means accessible & 'blank' means not accessible.


Java packages

  • lang.
  • io.
  • util.
  • applet.
  • awt.
  • net.

hashCode()

The hashCode() method is a Java Integer class method which returns the hash code for the given inputs. It basically gives a random number .


Some points of abstract method

  • In abstract method we can't create static constructor.
  • We can't create abstract static method.
  • We can create static method inside abstract class.
  • If one of the method is abstract then class also needs to be abstract as well. ___

Interface

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface.


Final

The final keyword in java is used to restrict the user.
If you make any variable as final, you cannot change the value of final variable.


Some points for static interface method

  • Static interface method should always have body.
public interface A {

    static void greeting(){
        System.out.println("I am in A");

    }
}
Enter fullscreen mode Exit fullscreen mode
  • We have to call it via interface name.
public class Main {
    public static void main(String[] args) {
         // Here we created a object
         Main obj = new Main();
                 // Here we called it via interface name
         A.greeting();
    }
}
Enter fullscreen mode Exit fullscreen mode

Nested Interface

An interface, i.e., declared within another interface or class, is known as a nested interface.

  • A nested interface declared within an interface must be public.
public class A {
    public interface NestedInterface {
        boolean isodd(int num);
    }
}


class B implements A.NestedInterface {

    @Override
    public boolean isodd(int num) {
        return (num & 1) == 1;
    }

}
Enter fullscreen mode Exit fullscreen mode
  • A nested interface declared within a class can have any access modifier

These are some OOP concepts. 😀

Top comments (0)