Object-Oriented programming

Object-Oriented Programming

Any programming language that supports below futures then that language is known as object-oriented programming.

  1. Class
  2. Object
  3. Polymorphism
  4. Inheritance
  5. Encapsulation

We will discuss each of these topics here:

Best Selenium Online Training

Our Trainer Profile

Class:

A class is a combination of data members and functions which has common properties.

How to create class syntax:

Class_Name

{

Data members

Data functions

}

Ex: class Car

{

String steering;

String break;

int wheels;

void run()

{

}

void reverseGear()

{

}

}

 

Objects:

The object is an entity that has a state and behavior is known as an object e.g. Pen, Fan, bike, car, etc. It can be physical or logical.

Syntax for creating object : Class_Name objname = new Class_Name();

Ex: Car maruthi = new Car();

Car Skoda = new Car();

Car BMW=new Car();

Here maruthi, skoda & bmw are the objects of the class Car.

Polymorphism:

Polymorphism is a biological word means that many forms. More than one function will have the same name.

Polymorphism can be implemented in two ways

  1. Compile-time polymorphism:
  2. Run time polymorphism:

Compile-time polymorphism: is implemented using method overloading and constructor overloading

Run time polymorphism: is implemented using function overriding with use of keyword called super.

Best Selenium Online Training

Our Trainer Profile

Inheritance:

Inheritance is a mechanism in which one object acquires all the properties and behaviors of the parent object.

The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you reuse (or inherit) methods and fields, and you add new methods and fields to adapt your new class to new situations.

Why use Inheritance?

  • For Method Overriding (So Runtime Polymorphism).
  • For Code Reusability.

Syntax of Inheritance

class Subclass-name extends Superclass-name

{

//methods and fields

}

The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a superclass. The new class is called a subclass.

Types of Inheritance

  1. Single
  2. Multilevel
  3. Multiple
  4. Hybrid

Java does not support multiple inheritance however it can be implemented using Interfaces.

Single Inheritance:

class Father

{

//methods and fields

}

class Child extends Father

{

//methods and fields

}

Multilevel Inheritance:

class GrandFather

{

Void login()

{

}

}

class Father extends GrandFather

{

//methods and fields

Void transferFunds()

{

 

}

Void raise ChecqueBookRequest()

{

}

class Child extends Father

{

//methods and fields

}

Best Selenium Online Training

Our Trainer Profile

 

Leave a Comment

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

18 Years Experienced Trainer