OOPS Concepta

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

// class declaration

public class Dog


{

//Instance variable

String name;
String breed;
Int age;
String colour;

Public Dog (String name, String breed, int age, String colour)
{
this.name = name;
this.breed=breed;
this.age=age;
this.colour=colour;
}
//Method 1

Public String get Name()


{
Return name;
}
//Method 2

Public String get Breed()


{
Return breed;
}

//Method 3

Public String get Age()


{
Return age;
}
//Method 2

Public String get Colour()


{
Return colour;
}
Public static void main (Strings[] args)
{
Dog tuffy=new Dog(“tuffy”, “Pomarion”, 9, “white”);
System.out.println(tuffy.toString());
}
Class
A class is a collection of objects that defines a set of properties that is common to all objects of a particular
type. It can also be called a blueprint for creating objects. A class entails the following components:

Class name: The name given to a class starting with a capital alphabet.

Modifiers: Based on the functionality of the class modifiers can either be public, private or default.

Body: The class body contains all the codes on the objects present in the class. This could range from declaring
any variables or creating any constructor or methods that contain the functioning of an object.

Object
An object is defined as an instance of a class and contains real-life entities. For instance, for a class called
Animals, Its objects will be a cat, dog, elephant et al. Each object has its own identity, attribute, and behaviour.
The code below depicts the use of class, object, and method while programming in the java language.

Methods
Methods are defined within a class and are used to perform a specific function. The method may or may not
contain an input parameter. The code below depicts the use of class, object, and method while programming in
the java language.
In the above code, Player is the name given to our class, whereas runs is a parameter passed in the method
Batsman which returns the runs scored by him when called via an object called myobj.

Access Modifiers
The access modifiers in Java defines the accessibility or extent of a method or constructor or the class. The four
types of access modifiers are:

1. Public: The code written within a class is accessible to other classes.


2. Private: The code written is only accessible within that specific class.
3. Default: The code written is accessible within the same package.
4. Protected: The code is accessible within a package and also through a subclass. In the absence of a
child class, the code cannot be accessed.

Now let’s proceed and talk about the crux of object-oriented programming.

Inheritance
The term inheritance refers to inheriting the properties of one class to another. The properties refer to the
attributes and methods of the parent class. The parent class is that class whose properties need to be inherited by
other classes. The classes that inherit the properties of the parent class are called the child class or subclass. To
inherit the properties of the parent class into the child class, a keyword called extends is used.
In the above example, the Sponsor is the parent class with the owner being its attribute. We have created a
subclass called Team that inherits the parent class- Sponsor. We have created an object of Team that can access
the properties of the parent class. The output of the above code is:

Rakuteen Barcelona

Polymorphism
As the name suggests- Polymorphism is the ability of a variable or a function to exist in multiple forms.
Polymorphism allows the programmer to perform different tasks using the same variable or function. A real-life
example of polymorphism would be- consider an open ground, now this ground can be used for playing sports.

Besides, it could also be used to organize weddings and concerts. Lastly, the same ground can be used for
parking vehicles. From this, we can infer that a single variable can have multiple implementations depending
upon its usage.

The polymorphism we usually come across two terms namely- Method overloading and Method overriding.

In Method Overloading, a single method can be used in numerous ways and perform different functions. The
methods will have the same name but different parameters can be used as input.

In Method Overriding, the method of the parent class can be overridden by the child class. With this, the same
method can perform differently when invoked by the parent class and by the child class.

An example of the polymorphism is shown below:


In this example, using the same method we can perform multiple tasks. The same method Voice when used in
Bird would output “Turr Turr” and when used with Duck will output “Quack Quack”. The snapshot of output is
shown below-

Abstraction
Abstraction is the process of hiding certain data from the users and showing only the required information to
them. For instance, while driving a car, we are not concerned about internal functions or mechanisms. What is
shown to us are the speed at which the car is being driven and the litres of petrol available. All the other
marginalized data are not displayed to the driver.

The abstract keyword is used for methods and classes while performing abstraction. For an abstract class, we
cannot create an object while the abstract method should not include a body. If any of the two rules are violated,
the output will generate an error.
Here, we have created an object of the subclass- Duck which is inherited from the main class- Bird. The output
is shown below:

quack quack
Zzz

Encapsulation
Encapsulation is the process of binding the code and the data together into a single unit. Here, the variables of a
class are hidden from other classes (by using the keyword private) but can only be accessed through a member
function. Setter and getter functions are used to access the private variables of a class that is abstract.
Until now, we have covered anything and everything that is related to object-oriented programming using Java.
Before we conclude, let us look at some of the advantages of the OOP concept.

 The code can easily be reused and therefore saves a lot of time and cost for the development of codes.
 It helps in designing the code in a well-structured manner so that any new programmer does not have to
spend long hours to understand the code.
 Besides helping users in writing code efficiently, it makes sure that security is not compromised.

Advantage of OOPs over Procedure-oriented programming


language
1) OOPs makes development and maintenance easier, whereas, in a procedure-oriented programming
language, it is not easy to manage if code grows as project size increases.

2) OOPs provides data hiding, whereas, in a procedure-oriented programming language, global data can be
accessed from anywhere.

Figure: Data Representation in Procedure-Oriented Programming


Figure: Data Representation in Object-Oriented Programming

3) OOPs provides the ability to simulate real-world event much more effectively. We can provide the
solution of real word problem if we are using the Object-Oriented Programming language.

You might also like