Important OOP Features in Java

Some Features in OOP That You Must Know About — Simply Explained in Java

Dinusha Chandrakumaran
LinkIT

--

Photo by Valeria Boltneva from Pexels

It is important to understand the concepts of attributes, methods, object, constructor, constructor overloading, static members, inheritance, and interface to understand the advanced oop concepts in Java. So let's dive into the basic concepts that you want to know before learning the advanced oop concepts…

Attributes

Data can be encapsulated within the class body. These data are called attributes.

class Student{
String name; //attribute
String index; //attribute
int age; //attribute
}

Methods

Methods are functions within the class that is coded once and reused many times when needed.

class Student{
//Method
public void printData(){
}
}

Object

It is a piece of the memory block, and it is an instance of a class. Each object will have a copy of instance members (attributes, methods) of a class. While instantiating an object, the object will get a copy of instance members. But when only declaring an object, the object will be assigned with a blanked memory block.

Constructor

Constructor is a special type of method that is used to instantiate an object. This means getting a copy of all the attributes, and methods of a class to the object. Constructors are of two types, they are default and user-defined constructors.

By default, every java class has a default or no-argument constructor.

class Student{
public static void main(String[] args) {
//Creating a Student object using default constructor
Student s = new Student();
}
}

User-defined constructors are used to instantiate an object with the user's own data sets rather than using the default constructor.

class Student{
String name;
//User defined constructor or argumented constructor
Student(String studname){
this.name=studname;
}

public static void main(String[] args) {
//Creating a Student object using user-defined constructor
Student s = new Student("Dinusha");
System.out.println("Name : "+name);
}
}

Constructor Overloading

use constructor overloading to instantiate objects with different types of arguments to handle multiple types of objects.

class Student{
String id,name;
int age;
double fees;
Student(String id,String name,int age, double fees){
this.id=id;
this.name=name;
this.age=age;
this.fees=fees;
}

Student(){}

Student(String id, double fees){
this.id=id;
this.fees=fees;
}
Student(String id,String name){
this.id=id;
this.name=name;}
}
Student(String id,String name){
this.id=id;
this.name=name;}
}

Static members

Static variables and static methods are called static members. Static members are created when a class is compiled and destroyed when a class is deleted from the memory. Static members are common for all objects of a class.

Inheritance

Inheritance is used to perform system maintenance, without making changes to the existing system. It is a process of creating subclasses from its parent class. Most of the attributes and methods can be accessed by sub-class from its parent class.

Java supports single inheritance, hierarchical inheritance, and multi-level inheritance.

Single inheritance means creating a sub-class from its parent class. Hierarchical inheritance means two or more sub-classes extend their parent class. Multi-level inheritance means at least 3 levels of classes are created.

Given below is an example of Single Inheritance

class Employee
{
String id,name;
Employee(String id, String name)
{
this.id = id;
this.name = name;
}
}
class Manager extends Employee
{
String division;
Manager(String id, String name, String division)
{
super(id,name);
this.division = division;
}
}

Given below is an example of hierarchical inheritance

class Student
{
String id,name;
Student(String id,String name)
{
this.id = id;
this.name = name;
}
}
class LawStudent extends Student
{
String duration,proj_title;
LawStudent(String id,String name,String duration,String proj_title) {
super(id,name);
this.duration = duration;
this.proj_title = proj_title;
}
}
class ItStudent extends Student
{
int mar1,mar2,mar3,total;
double average;
ItStudent(String id,String name,int mar1,int mar2,int mar3)
{
super(id,name);
this.mar1 = mar1;
this.mar2 = mar2;
this.mar3 = mar3;
}
}

Given below is an example of multi-level inheritance

class Employee
{
String id,name;
double salary;
Employee(String id,String name,double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
}
class Manager extends Employee
{
String division;
double bonus,grossSalary;
Manager(String id,String name,double salary,String division)
{
super(id,name,salary);
this.division = division;
}
}
class AssManager extends Manager
{
double allowance,netSalary;
AssManager(String id,String name,double salary, String division)
{
super(id,name,salary,division);
}
}

Interface

The interface is also like classes, but it will have final variables and abstract methods other than classes. The final variables are constants and the abstract methods are only the method declaration. You can get an idea about the interface from the code given below.

interface Tax
{
final double taxA = 0.1; //final variables
final double taxB = 0.05; //fina variables
public void showTax(); //abstract methods
}

Conclusion

In this article, I have discussed the basic concepts of object-oriented programming concepts in Java. I hope this article will help you to learn the advanced concepts of OOP easily.

--

--

Dinusha Chandrakumaran
LinkIT
Writer for

Undergraduate at Faculty of Information Technology, University of Moratuwa