Learn oop Concept In Java

⇒Object oriented concepts in java :-Object oriented programming Structure or system is the extended form of OOPs.Paradigm can also be termed as method to solve some problem or to do some task.

Programming paradigm is an approach to solve problem using some programming language or also we can say it is a method to solve a problems using some tools and techniques. In OOps is way by help of which many problems can be solved. In java many variables and methods are used and these methods and variables.OOPs main concepts or pillars Object oriented concepts in java :-

  1. Class
  2. Objects,Methods
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

class and object in oops

⇒Class :- OOPs concepts can be understood easily if these concepts are related to real world.Now class is a a main OOP pillar if we relate class with real world then it can be considered as a category of some objects like an Animal class which is having cats, dogs,lions etc. in this or a class of birds like peacock,sparrows etc. So in this the main categorization is called class like animal, birds or vehicles. In terms of java class can be defined as collection of objects.It is not a real world entity and it is just a template,blueprint or prototype.lass does not occupy memory because a class like animal but its objects like cats,dogs can occupy memory.

Syntax:-

class Animal

{

}

⇒Methods :-  A method can be defined as set of codes or instructions which perform a particular task.The methods can be used agin and agin in a particular code.

Advantages of metods

  • Code reusability
  • Code optimization

Syntax:-

access modifier return-type Methodname(list of parameters)

{

}

⇒Object:- It is an instance of class.Like above ‘Animal’ is considered as class and ‘Dog’ is an instance of animal class. It is a real woirld entity.Objects also occupy memory.Objects also consists of their own identity.

Objects consists of

Identity-Name
States/attribute-Like color,breed or age of dogs etc.
Behaviour-eat,run
How to create an object

By using new keywords

Declaration:- ClassName objname
         Animal buzo

Instantiation:- objname=new
buzo=new

Initialization:-objname=new Animal();
buzo=new Animal();

SyntaxAnimal buzo= new Animal();

⇒ Initializing object by refrence variable :-

class Animal

{

String color;

int age;

public static void main(String[] args)

{

Animal buzo=new Animal();

buzo.color="black";

buzo.age=10;

System.out.print(buzo.color+" "+bozo.age);                          

}

}

⇒ Output :

⇒ Output :black 10

⇒ Initializing object by using method:-


class Animal

{

String color;

int age;

void initObject(String C,int a)

{

color=c;

age=a;

}

void display()

{

System.out.print(color+" "+age)

}

public static void main(String[] args)

{

Animal buzo=new Animal();

buzo.initObject("black",10);

buzo.display();                   

}

}

⇒ Output :

black 10

Constructor in Java

⇒ Definition :-

initialize an object immediately upon creation.constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.Automatic initialization of of objects when they are created is done by

Rules for writing Constructor:

Constructor must has same name as the name of the class.
Constructor does not have any return type like return etc.
Access modifiers can be used in constructor declaration to control its access i.e accessibility of constructor in other class
A constructor in Java can not be abstract, final, static and Synchronized.
Representation of constructor

class Test

{

//This is constructor

public Test() // Having same name as of the class.

{

// code

}

}

⇒Need of constructor:-

Constructor is the first method that is invoke when you create an object.suppose that we an class of Employees. If we have to include name and id of each one then for that we have to initialize each variable every time whenever any new employee is added.But there is problem in this approach i.e, if we have 1000’s of the employee then there variables need to declare 1000’s time. Lets see this by an example.

class Employee

{

String name;

int id;

public static void main(String[] args)

{

Employee e1 = new Employee()

t.name="Ram";// similarly we have to do for 1000's of the Employee

t.Id=1;// similarly we have to do for 1000's of the Employee

System.out.println("Name is "+t.name);

System.out.println("Id is "+t.Id);

}

}

⇒ Output :

Name is Ram

Id is 1

But this is not an efficient approach because it consumes more time and sign of bas programming .For this problem we have solution that is a constructor which we are going to use.

Program for constructor in java

class Employee

{

String name;

int id;

public Employee(String name,int id) // Having same name as of the class.

{

this.name = name;

this.id = id;

System.out.println("name is "+name);

System.out.println("Id is "+Id);

}

public static void main(String[] args)

{

Employee e1 = new Employee("Ram",10); // By this we can initialize many of the Employees

Employee e2 = new Employee("Rahul",20);

}

}

⇒ Output :

name is Ram

Id is 10

name is Rahul

Id is 20

So constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).

⇒ Types of Constructor :-

We have three types of constructor in java

1. Default Constructor(no-arguments)

2. no-argument Constructor(User defined)

3. Parameterized Constructor

⇒Default Constructor –

Default constructors are the type of constructors which are not created by the programmers during the program. But whenever any object is created the compiler by itself create an default constructor.Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class

Note:

Default constructor is only created by the compiler if it is not created by programmer.The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type

class Employee

{

String name;

int id;

public void display // Having same name as of the class.

{

System.out.println(id+" "+name);

}

public static void main(String[] args)

{

Employee e1 = new Employee();

Employee e2 = new Employee();

e1.display();

e2.display();

}

}

⇒ Output :

0 null

0 null

⇒ User defined constructor-

It is the type of the constructor which is created by the programmer by itself with no arguments. It is also called as the no argument constructor.

class Employee

{

public Employee() // constructor with no arguments

{

System.out.println("I am user defined constructor");

}

public static void main(String[] args)

{

Employee e1 = new Employee();

}

}

⇒ Output :

I am user defined constructor

⇒ Parameterized constructor:-

A constructor that has parameters is known as parameterized constructor.We can initialize the class fields by own value.

⇒ Output :

I am parameterized constructor

Leave a Comment