Table of Contents
In this blog, we will go over the fundamentals of OOPs concepts in Java. OOPs is a language given to program the user to build software that rests on classes and objects. we modularize the real world, by implementing and inheriting things together. an object is a combination of data that has data members and functions. oops uses different concepts such as abstraction, polymorphism, inheritance, and encapsulation. maintenance, reusability, and adaptability are the advantages of oops in java.
What is Oops?
OOPs is a computer language used to design and build software with objects and classes. Data and attributes are the concepts of objects. The basic unit of programming is an object. each object has certain attributes and exhibits different behavior.
What is OOPs in java?
Java is a secured programming language developed by sun microsystem. James Gosling is the founder of Java. OOPs is a primary source code to implement the data in the system. it also manipulates data that represents to focus on the object data.
What is OOPs concept in java?
Programming’s essential tenets based on object-oriented programming (OOP) ideas. The goal of OOPs in Java is to increase code readability and reuse by defining Java programs.
Java datatype and variable
What is a data type?
A data type is a variable that stores in the form of size and values.
Declaring a variable
int u,v,w;
Char z;
Initialize a variable
x=”z”;
y=2
Now it is int a=5,b=8
Variable types of oops in java
1. local
2. instance
3. static
Example
Class main
Static int x=10;
Int data=50;
Void method()
Int y= 20;
}
}
OOPs concept in java
- Objects
- Classes
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism
Object
Programming’s fundamental building block is an object. Object recognizes their distinctive names. an object defined as a collection of data members and operations also called methods.
For example, a car is an object which has attributes like color, model, and year of manufacturing. and its functions start, stop, and move.
Classes
A class can form by grouping objects with similar properties.
Example: we have a Class of Cars in which Santro Xing, Alto, and WaganR represent individual Objects.
In this context, each Car Object will have its Model, Year of Manufacture, Color, Top Speed, Engine Power, and so on, which form Car Class Properties, and the associated actions, i.e., object functions like Start, Move and Stop, which form Car Class Methods.
Abstraction and encapsulation
Grouping of data and methods into a single entity known as data encapsulation or data hiding.
Encapsulation is a data hiding used to protect data from the outside world.
Inheritance
It is extending an existing class to create a new one. Inheritance is the process by creating a new class by which objects of one class inherit the properties of objects of a different class.
polymorphism
A polymorphic operation displays various behavior depending on the situation. The type of data used in the process, which determines the behaviour.
Benefits of OOPs in java
- The data-hiding principle assists programmers in creating secure programs.
- We can drop code repetition and extend the use of existing classes by using inheritance.
- It is simple to divide work in a project into objects.
- Object-oriented systems scale well from small to large systems.
- Software complicity is easily manageable.
Concepts of OOPs comparison with different programming languages using an example
An illustration of how java’s oops principles differ from other programming paradigms
Suppose you want to create a new account with functions like Account number, account balance.
First, we will apply this in an unstructured programming language. Unstructured programming languages were the first of all programming languages.
Int account_num=5;
Int account_bal=1000;
Again a deposit made
Account_bal=account_bal+1000
Display the account balance
printf(“account_num=%d,account bal”)
printf(“account_bal=%d,account bal”)
Now withdraw 500
account_bal=account_bal-500
Now again you need to display the account balance
In this unstructured program, codes will get repeated again and again
We will apply the same code to Structured programming
printf(“account_num=%d,account bal”)
printf(“account_bal=%d,account bal”)
Void showdata()
Here we use void call data to call the function itself. whenever needed it calls a function
The same code we will apply in oops concepts(object-oriented programming)
Class account
{
Int account_num;
Int account_bal;
Public void showdata()
{
system.out.println(“account num”+account_num)
system.out.println(“account bal”+account_bal)
}
}
A software program is an fundamental aspect which has the ability to conduct specific operations on data and have data available. The idea of merging data and operations came from software programming experts. Object Oriented Programming, or concepts of OOPs in java, was thus born.
Int account_num=5;→DATA
Int account_bal=1000;
Void showdata()→ACTIONS
OOPS concepts in java with example
Objects & classes
OOPs is a concept of java in a class, all of an object’s functions and data converted to user-defined data types. A class is a “data type,” and an object is a “variable” of that type. After a class created, the maximum amount of its components get constructed.
Syntax
Access modifier class<class name>
{
Data member;
Method;
}
public class School
{
String x="henry harvin";public static void main(String[] args)
{
school myobj=new school();
System.out.println(myobj.x);
}
}
Create the class school. To create an object, specify the class name, followed by the object name, and use the keyword new.
school Myobj= new school ()
Created a java file called myschool.java and prints henry harvin on the screen.
You can also create many objects in the same class
Public class myschool
{
string x=henry harvin;
string y=yyy;
}
Class courses
{
Public static void main (string [ ] args)
{
Myschool myobj1=new myobj1();
Myschool myobj2=new myobj2();
system.out.println(myobj1.x);
system.out.println(myobj2.y);
}
}
Points to remember
- Java requires that every line of code run within a class. We gave the class in our example the name Myschool. Every class should begin with an uppercase letter.
- Java is case-sensitive: “My school” and “school” has a different meaning.
- The main() method will run any code inside of it. The keywords before and after the main are unimportant.
- We can print a line of text on the screen within the main() method using the println() method.
- The curly braces indicate the beginning and end of a code block.
- Here system in out represents output a component of the built-in Java class. A value printed on the screen using the println() method, short for “print line” (or a file).
- Each statement must end with a semicolon.
Inheritance
The “superclass” is the existing class and the “subclass” is the new class. superclass derived from the child class and subclass derived from the parent class.
Types of inheritance
- Single inheritance
- Multi level inheritance
- Hybrid
- Hierarchical inheritance
- Multilevel inheritance
Single inheritance
Syntax
Class derived- class extends base-class
{
//methods and fields
}
Example
class school
{
void name()
{
System.out.println("course...");}
}
class course extends school
{
void place()
{
System.out.println("place...");
}
}
class TestInheritance{
public static void main(String args[]){
course d = new course();
d.name();
d.place();
}
}
Multilevel inheritance
Multilevel inheritance occurs when there is a chain of inheritance.
class school
{
void name()
{
System.out.println("name");
}
}
class course extends school
{
void place()
{
System.out.println("place");
}
}
class primaryschool extends course
{
void course()
{
System.out.println("course");
}
}
class Testinheritance2
{
public static void main(String args[])
{
primaryschool d=new primaryschool();
d.course();
d.place();
d.name();
}
}
Hierarchical inheritance
Hierarchical inheritance occurs when two or more classes inherit the same class.
Example course and primary school inherits the school class,so there is hierarchical inheritance.
class school
{
void name()
{
System.out.println("name...");
}
}
class course extends school
{
void course()
{
System.out.println("course...");
}
}
class primaryschool extends school
{
void nursery()
{
System.out.println("nursery...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
primaryschool d=new primaryschool();
d.name();
//d.course();
d.nursery();
}
}
Hybrid inheritance
Hybrid inheritance is a composition of two or more of the preceding types of inheritance.java does not support hybrid inheritance.it can achieve only through interface.
Example
class School
{
}
class primaryschool extends School
{
}
class middleschool extends School
{
}
class highschool extends primaryschool
{
public static void main(String args[])
{
School s = new School();
primaryschool p = new primaryschool();
middleschool m = new middleschool();
System.out.println(s instanceof School);
System.out.println(p instanceof primaryschool);
System.out.println(m instanceof School);
}
}
- Hybrid inheritance in Java combines two or more inheritance types. Java uses defined classes in the software to reuse the code and modularize the software. Uncertainty avoided doing this. example: class B extends classes A and c, and A and c have the same method display ().
- Java’s compiler is currently unable to choose which display method to inherit. Java forbids inheritance to prevent this from happening.
- Multiple inheritance is not supported in Java, hybrid inheritance is not possible. This necessitates the usage of interfaces, just like multiple inheritances.
Multiple inheritance
Multiple inheritance is not achieved through class but supported through interfaces.
Public interface interfaceA
{
Public void hello();
}
Interface b.java
Public interface interface
{
Public void hello();
}
As seen in the example, we make an interface to extend one of these interfaces and to specify the same function.
InterfaceC.java
Public interface interfaceC extends interfaceA,interfaceB
{
Public void hello();
}
- Declaring a method with the concrete class will implement the interface which is totally acceptable to create an interfaces.
- Hence, multiple inheritances in Java are not subject to form any ambiguity. Because of this, a Java class may implement a number of interfaces.
Abstraction
An abstract class is one that has the keyword abstract in its declaration.
Syntax
Abstract class main()
{
Public abstract void hello()
}
Declaring an Abstract class
Abstract class a()
Declaring an Abstract method
Abstract void hello()
abstract class school
{
abstract void name();
}
class primary extends school
{
void name()
{
System.out.println("good");
}
public static void main(String args[])
{
school obj = new primary();
obj.name();
}
}
Example 2:
abstract class Bank
{
abstract int getRateOfInterest();
}
class SBI extends Bank
{
int getRateOfInterest()
{return 5;
}
}
class PNB extends Bank
{
int getRateOfInterest()
{
return 6;
}
}
class TestBank
{
public static void main(String args[])
{
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}
}
Points to remember
- An abstract method is declared as an abstract class but has no implementation.
- A data member
- abstract method
- method body (non-abstract method),
- function Object() { [native code] }
- the main() method can found in an abstract class.
- The interface can also implement in some way using the abstract class. In this situation, the user may not required to override every interface method.
Encapsulation
Data encapsulation restricts the class fields and methods and modification by exterior classes. Moreover, this aids in data protection.
Example
Public class school
Private string Name;
Public string getName()
{
Return name;
}
Public void setName(String name)
{
this.name=name
}
}
There are two methods in abstraction.
“read-only” and “write only” called getter-setter methods in java. It provides control over the data.
certainly, if you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method.
You can write the logic not to store the negative numbers in the setter methods.
Example of getter and setter method in encapsulation
class school
{
private int name;
private int regno;
// getter method
public int getregno()
{
return this.regno=num;
}
// setter method
public void name
{
this.name;
}
}
Polymorphism
Here we have two types of polymorphism
1. Runtime
2. Compile time
Example for runtime polymorphism
Public class school
Public void class ()
{
system.out.println(“school is good”);
}
}
primary.java
Class primary extends school
{ //override//
Public void class()
{
system.out.println(“nursery”);
}
Public static void main(string args[])
{
School obj=new primary();
obj.class();
}
}
It shows the output as nursery
Middle.java
Class middle extends school
{ //override//
Public void class()
{
system.out.println(“6to8”);
}
Public static void main(string args[])
{
School obj=new middle();
obj.class();
}
}
It shows the output as 6to8
We defined the method sound() and have many implementations of it in the various sub-classes, as seen in the sample above.
The example we provided below is a runtime polymorphism
Class school
{
Void demo (int a)
{
system.out.println(“a:” +a);
}
Void demo (int a,int b)
{
system.out.println(“a and b:” + a+”,” + b);
}
Second demo (second a)
system.out.println(“second a:”+a);
Return a*a;
}}
Class method
{
Public static void main(string args[])
{
Method obj=new method();
Second result;
obj.demo(2);
obj.demo(2.4);
result=obj.demo(4,4);
system.out.println(“output:” + result);
}
}
There are three overloads of the method demo() in this case the first has one int parameter, the second has two int parameters,third has a double parameter.
To specify the method we call the arguments to supply.Compile time polymorphism is the name given to the type of polymorphism that occurs when code is being built at runtime.
Conclusion
OOPs concepts in Java.
- OOPs is a concept created to overcome the problems of unstructured programming.
- There are many languages that use oops like c,c++, and java.
- Some of the advantages of using oops is the maintenance of complicated programs and reusability.
- The Java Class is a component that controls the behavior and content of Java objects.
- A Java object is a standalone element that has methods and properties to make a certain type of data usable.
- Using techniques like inheritance, overriding, and augmenting, a class system enables the computer to define a new class (derived class) in terms of an existing class (superclass).
Best institute to learn Java for beginners.
- Most respected industry experts with 11+ years of working experience.
- Access E-learning access to tools and techniques,video content,assessment.
- Find100%placement for 1 year post successful completion.
- Get Hallmark Certification of Certified Java Developer from Henry Harvin® Govt of India recognized & Award-Winning Institute,and showcase expertise.
- Understand the core Fundamentals of Java Programming language.
- Acquire knowledge in Java Installation.
- Learn Syntax to build useful applications.
- Build necessary Tools used in Java applications.
- Learn how Java Codes gets executed.
1. What is OOPs?
OOPs is a computer language used to design and build software with objects and classes. it relies on the concept of an object which is data, or attributes. an object is the basic unit of programming. each object has certain characteristics and exhibits different behavior.
Objects
Classes
Abstraction
Inheritance
Encapsulation
Polymorphism
OOPs, concepts in java explain objects,
classes, abstraction, inheritance, polymorphism, and encapsulation with syntax and examples.
Programming’s essential tenets based on object-oriented programming (OOP) ideas. The goal of OOPs in Java is to increase code readability and reuse by defining Java programs.
The data-hiding principle assists programmers in creating secure programs.
We can drop code repetition and extend the use of existing classes by using inheritance.
It is simple to divide work in a project into objects.
Encapsulation is a data hiding used to protect data from the outside world.
Learn java programming course for beginners
Recommended for you