Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

OOps Concept in Java Programming

OOPs concept in Java programming is based on the notions of abstraction, encapsulation inheritance and polymorphism. Users create objects and then come with methods for dealing objects. The fundamental idea behind OOPs to create objects, reuse them in the program and then manipulate the objects to produce results.

OOPs has information along with it’s behaviour(methods) in one location(object) helps to comprehend the way a program operates. We will go over every aspect of Object Oriented Programming in depth to ensure you don’t have problems understanding OOPs Concepts.

OOPs Concept in Java

1) Class

The collection of objects is known as class. It is a logical concept. It is among the basic OOPs concepts that is a collection of similar entities. Class is merely a logical element, not the physical thing. Let’s take a look at the concepts of OOPs by comparing to you have class “Expensive Cars”. Its properties(data) may be related to price or the speed of these vehicles. The methods can be used with vehicles include reverse, driving and braking, among others.

2) Object

An object may be defined as an instance the class. There could be many instances of a class within an application. The term “object” is one of Java programming OOPs concepts, which includes the data as well as the function that operates upon the information. An object can be defined by referring to it as an example of an class. A class contains an address and occupies some memory. They can be in communication without knowing the specifics of their respective data or codes. For example – chair, bike, marker, pen, table, car, etc.

3) Polymorphism

When a particular task is completed in various ways, it’s called polymorphism. Examples include: convincing the client in a different way, or to draw something like the shape, triangle or rectangle, for example.

In Java the method is used for overloading as well as method overriding to create polymorphism.

Another way to demonstrate this is to communicate something, for example, a cat says meow, a dog barks woof, etc.

Polymorphisms in Java is Two Types:

Overloading

Overriding

4) Abstraction

Abstraction in Java OOPS represents the most important aspects without including background information. Abstraction of data can be described as the feature that exists by means of which only necessary information is shown to the user. The unnecessary or insignificant units aren’t displayed for the users to see.

It’s a method for creating a new type of data which is suitable to be used in a specific way. Let’s look at one of OOPs concepts in Java with an example of driving in a vehicle, you don’t need to worry about the internal workings of the vehicle. Example: A car is considered to be a car, instead of its components

Abstract class syntax:

Abstract class Mobile { // abstract class mobile

Abstract void run(); // abstract method

Therefore, we’ve extracted, fetched and selected the information about students, from the massive data. This is known as abstraction in oops concept. Abstract Methods and classes in OOPs concepts

Abstract method:

A.) The method is officially declared but not identified. The only method signature is the body.

B.) Declared using an abstract keyword

Example :

abstract public void playInstrument();

Useful to enforce an obligation on the class that inherits the class to implement abstract methods. The inheriting class has to be able to implement the abstract methods used by the parent class, or declare the subclass abstract.

 They are not abstract

Constructors

Static methods

Private methods

The methods that have been declared “final”

Abstract Class

A class abstractly outlines the techniques, but it does not have to implements all of the methods.

{abstract class AAbstract class A

abstract void myMethod();

Void anotherMethod(){the method.

Does something happen?

}

}

Note 1: It may be situations in which it is not possible to implement all of the methods within the basic class. In these scenarios, it is possible to describe this base class to be an abstract class , which means that the base class can be described as a particular type of class that isn’t complete by itself.

A class that is derived from the abstract base class has to implement the methods that aren’t implemented(means that they’re abstractions) within the abstract class.

5) Encapsulation

Encapsulation is among the most effective Java OOPs concepts for wrapping code and data. In this OOPs idea the variables in the class are never visible by other classes. In encapsulation, information in a class is separated from other classes. This is sometimes referred to as data-hiding.

 The process of combining data and code to form a single unit is called Encapsulation. It is only accessible through the methods that are available in the class they are currently in. For instance, capsules, they are filled with various medications.

Encapsulation can be accomplished by declaring all variables within the class as private, and then creating public methods in the class to determine and set the value of variables.

6) Inheritance

The method by which one class gains the properties and capabilities of another is known as inheritance. Inheritance is a way to ensure the reusability of code. Each subclass defines only the specific features specific to that particular class and the other features may be acquired in the same parent.

A.)  Inheritance is the process of defining a brand new class on top of an existing class by expanding its data members that are common to all classes and techniques.

B.)  parent class can be referred to as the super class or base class. The child class which extends from the parent class known as the sub class or child class.

C.) Inheritance is a good way to define “reusability”, i.e. when we wish to develop an entirely new class, and there’s already a class with certain of the code we’d like to use, we could create our new class by utilizing the existing class.

D.) The main benefit of inheritance lies in the code from the base class does not need to be altered within the class of child.

The variables and methods used in the base class may be utilized in the child class, too.

Syntax

Class A extends to B

{

}

Inheritance Example

Here is an instructor for the parent class and a child class. ScienceTeacher class, we do not need to write the same code that is already used in the current class. We have the college’s name as well as a designation and() method that is used by all Teachers.ScienceTeacher classes do not have to write code. Data members are common to all classes and methods are available to inherit by class of Teachers.

{class Teacher Class Teacher

String designation = “Teacher”;

The string college is “Devapoint”;

void does(){

System.out.println(“Teaching”);

}

}

Public class ScienceTeacher extends {TeacherTeacher’s

MainSubject String is “Science”;

public static public static main(String parameters []){

MathTeacher obj = new ScienceTeacher();

System.out.println(obj.college);

System.out.println(obj.designation);

System.out.println(obj.mainSubject);

obj.does();

}

}

Output:

Devapoint

Teacher

Science

Teaching

Note that multi-level inheritance is permitted in Java however, it is not allowed to have multiple inheritance.

Types of Inheritance:

Single Inheritance refers to an individual child and parent relation in which a class extends to another.

Multilevel Inheritance: is the parent and child class relationship in which classes extend the child class. For instance, class A extends class B, while class B extends C’s class.

Hierarchical Inheritance: It is the parent and child relationship between classes where more than one class extend an identical class. For instance class B is extended by class A while class C extends class A.

Multiple Inheritance is the term used to describe an individual class being extended by more than one class, which implies that the child class is composed of two classes as its parents. Java does not allow multiple inheritance. Read the full article here.

7) Association

The term “association” refers to a connection among two things. This is among the OOPs Concepts in Java which is the basis for the differentiability between objects. In other words, one object could be associated with one or with a variety of objects. There are four possible types of associations between objects. In the OOP idea every object has its own lifecycle, and each can be no owner. For example, a lot of students may be associated with one teacher, while one student may be associated with several teachers.

8) Aggregation

In this method each object has its distinct lifecycle. However, there is a sense of ownership so that the child object doesn’t be part of another parent object. Aggregation is the term used to describe the situation where an object has additional objects as part of its status. It’s a sign of the weak bond between objects.

9) Composition

Composition is a special kind of Aggregation. It’s also referred to as “death” relationship. Children objects do not have a lifecycle, so if a parents object gets deleted, all children objects will be automatically deleted. Let’s look at an for instance the house and rooms. A house can contain many rooms. One room isn’t able to be part of two homes. Therefore, if you remove the room belongs to house, house will be removed.

Cohesion in Java

Cohesion is a measure of how the techniques and attributes of a particular class are meaningfully and tightly connected to each in terms of how focused on completing one task that is clearly defined in the framework. Cohesion in java can be used to determine the extent of a class’s one, clear and focused responsibility. Classes that are more cohesive are better to have them in place to ensure code reuse.

There are two kinds of Cohesion

Low Cohesion

High Cohesion

What is Message passing in oops?

A single object isn’t necessarily a great idea. A software application has many objects. One object communicates with another object through invoking methods that are associated with the object. It’s also referred to as Method Invocation

Benefits of Object Oriented Programming

  • OOPs concepts in Java provide easy-to-understand and a clear , modular structure for programming.
  • The objects created by Object-Oriented Programs can be reused in different applications. Thus it saves significant development cost.
  • Large programs can be challenging to write, however If the designing and development team adhere to OOPS principles, they will be able to design with minimal defects.
  • It increases the modularity of programs since each object is distinct.
  • Enhanced productivity during software development
  • Increased software maintainability
  • Faster development sprint
  • Lower development costs
  • High-quality software
Published
Categorized as Java

Leave a comment

Your email address will not be published. Required fields are marked *