logow

oops concepts in java with simple codes and examples


What is Java oops concepts and what is the benefits of oops concepts?

Today we are going to cover following topics--
  1. classes and objects
  2. Inheritance
  3. Encapsulation
  4. Abstraction
Basically,java oops concepts are used to design software with real time objects and mainly used for security purposes.Security purposes means Java use mainly 6 features we discuss following to make software data secure rather than C-language.


OOPS
Java OOPS Concepts In Hindi 

What is Classes and objects?


C O
Justok By Amit 


Basically,A class is a user defined blueprint or prototype in  which objects are created. It represents the set of properties and attributes with real time examples.objects tells about us attributes and behaviour of a class and it's also a instance of a class. suppose you have a smartphone class and it's object is attribute - name Samsung and it's behaviour like a moving , singing , and calling.

In object creation we use new keyword that means new keyword allocate dynamically memory to the object of class is creates at run-time. 

Define a class and objects..

<access_modifier> Class <name>
{
//codes or statements
}

Now lets take a example.....







Output is....
moving
singing
calling

So, we don't use any keyword(if,else,do,while....etc.) as class name.


what is inheritance in java ?

Basically,Inheritance follows IS-A approach.This is a process in which one class acquires all properties and functionalities of another class is called inheritance in java.The main benefits of inheritance is code reusability that means if we declare some variables in parent class we easily use these variables in further sub-classes or child class.
 extends keyword use to acquired all properties in child class within the parent class.Object create in inheritance only with child class.

Example of inheritance in java....



Output is....
samsung is best smartphone

price of samsung smartphone is-5999.00


Types of Inheritance in Java.......

There are 4 types of inheritance in java..

1.) Single Inheritance: Refers to a child and parent class relationship where a class extends the another class.
that's means class have only one inheritance.

2.) Multilevel inheritance: refers to a child and parent class relationship where a class extends the child class. For example class samsung extends class Newphone and class Newphone extends class Anotherphone.

3.) Hierarchical inheritance: refers to a child and parent class relationship where more than one classes extends the same class. For example, class Newphone extends class samsung and class Anotherphone extends class samsung.


4.) Multiple Inheritance: refers to the concept of one class extending more than one classes, which means a child class has two parent classes. Java doesn't support multiple inheritance.for multiple inheritance we use interface.we discuss about interface in next post.

Encapsulation in Java Programming..

In simple language we say the Encapsulation is the process to wrapping of data and functions in a single unit.
For example- Medicine in capsule etc.

How to define or use Encapsulation in Java..

1.) Make the instance variable of class private that means it's not access directly in main method.You can create method to access it.

2.) To access these data we use getter and setter method.getter display the setter arguments or statements.


Why Use Encapsulation ? 

Suppose you make a company software to manage employees personal details and employees also access each other profile but not show all employees to each other salary.So that's why we use Encapsulation.This is main features of Java to hide details of particular details of person and anything.


Another Terms About Encapsulation..

1.) Encapsulation use approach read-only and write-only codes. 
2.) Better control of attributes and objects and provide excellent security. 


Example of Encapsulation in Java Programming..




Output is.....

Amit
50000


What is Abstraction in Java ? 

In simple words we say Abstraction means hiding the implementation details only show user's to essentials details.Non-essentials or unnecessary data are not shown by
user's.Only functionality of codes show to the user's side.Abstraction used in java by using abstract class and interfaces.

For Example -  All you are using a smartphone we only know how smartphone functions work means how someone call you and how you make a phone call but you don't know how internally or externally phone calls being connected.This is hiding details we follows in java abstraction. 

Important facts about Abstraction ...... 

1.) Abstraction class define with only abstract keyword.

2.) It can have abstract and non-abstract methods also.

3.) We can't create object of abstract class.

4.) we also use constructor and finalize variables or final methods in abstract class.

Examples of Java  Abstraction ......



Output is...... 

Someone calling you pick up-

Encapsulation v/s Abstraction 

Basically , Encapsulation hiding the particular details or information whereas , Abstraction hiding the implementation details to the user's side . 


#StayHome

So We definitely cover all 4 topics in that post we discuss remaining two Polymorphism , Constructor in next posts. if any problem tell me in comments......




Explanation By ----- Amit 

Post a Comment

0 Comments