Oops in Java
Oops in Java
2. Why OOPs?
The main advantage of OOP is better manageable code that covers the following:
1. The overall understanding of the software is increased as the distance
between the language spoken by developers and that spoken by users.
2. Object orientation eases maintenance by the use of encapsulation. One can
easily change the underlying representation by keeping the methods the
same.
3. The OOPs paradigm is mainly useful for relatively big software.
3. What is a Class?
A class is a building block of Object Oriented Programs. It is a user-defined data
type that contains the data members and member functions that operate on the data
members. It is like a blueprint or template of objects having common properties and
methods.
4. What is an Object?
An object is an instance of a class. Data members and methods of a class cannot be
used directly. We need to create an object (or instance) of the class to use them. In
simple terms, they are the actual world entities that have a state and behavior.
Eg. The code below shows is an example of how an instance of a class (i.e an
object ) of a class is created
#include <iostream>
class Student{
private:
string name;
string surname;
int rollNo;
public:
name = studentName;
surname = studentSurname;
rollNo = studentRollNo;
void getStudentDetails(){
cout << "The name of the student is " << name << " " << surname << endl;
cout << "The roll no of the student is " << rollNo << endl;
};
int main() {
student1.getStudentDetails();
return 0;
Output
The name of the student is Vivek Yadav
The roll no of the student is 20
//It has a private data member and getter and setter methods.
return name;
return rollNo;
this.name=name
this.rollNo=rollNo;
7. What is Abstraction?
Abstraction is similar to data encapsulation and is very important in OOP. It means
showing only the necessary information and hiding the other irrelevant information
from the user. Abstraction is implemented using classes and interfaces.
void eat()
{
System.out.println("The animal is eating.");
Animal()
System.out.println(
Goat()
cow.walk();
cow.eat();
goat.eat();
8. What is Polymorphism?
The word “Polymorphism” means having many forms. It is the property of some
code to behave differently for different contexts. For example, in C++ language, we
can define multiple functions having the same name but different working
depending on the context.
Polymorphism can be classified into two types based on the time when the call to the
object or function is resolved. They are as follows:
Compile Time Polymorphism
Runtime Polymorphism
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early
binding is the type of polymorphism where the binding of the call to its code
is done at the compile time. Method overloading or operator overloading are
examples of compile-time polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime
polymorphism is the type of polymorphism where the actual implementation
of the function is determined during the runtime or execution. Method
overriding is an example of this method.
– Method Overloading
// An example of method overloading
class Student {
String name,surname;
int rollNo;
System.out.println(name);
System.out.println(surname);
System.out.println(rollNo);
// Main Method
// Method Overloading
st.showStudentDetails("Ank");
st.showStudentDetails(3);
st.showStudentDetails("Ank","Na",3);
Output
The name of the student is Ank
the roll no of the student is 3
Ank
Na
3
– Method Overriding
// Method Overriding Example
// Base Class
class Parent {
// Inherited class
super.show();
System.out.println("Child's show()");
// Driver class
class Main {
obj.show();
Output
Parent's show()
Child's show()
class Student {
this.password = password;
}
}
user1.userName = "Vivek_Kumar_Yadav";
user1.setPassword("abcd@12345");
user1.userEmail = "abc@gmail.com";
The code is easier to maintain and Proper planning is required because OOPs is a
update. little bit tricky.
Modifying and updating the code is Modifying the code is difficult as compared to
easier. OOPs.
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early binding is
the type of polymorphism where the binding of the call to its code is done at the
compile time. Method overloading or operator overloading are examples of
compile-time polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime polymorphism is the
type of polymorphism where the actual implementation of the function is determined
during the runtime or execution. Method overriding is an example of this method.
1. Single Inheritance: Child class derived directly from the base class
2. Multiple Inheritance: Child class derived from multiple base classes.
3. Multilevel Inheritance: Child class derived from the class which is also
derived from another base class.
4. Hierarchical Inheritance: Multiple child classes derived from a single
base class.
5. Hybrid Inheritance: Inheritance consisting of multiple inheritance types
of the above specified.
// an example of single inheritance
class Father {
class Father {
A class that is abstract can have both An interface can only have abstract
abstract and non-abstract methods. methods.
An abstract class can have final, non-final, The interface has only static and final
static and non-static variables. variables.
class Student {
String name;
String surname;
int rollNo;
Student()
System.out.println("contructor is called");
}
}
String name;
String surname;
int rollNo;
Student()
}
}
3. Parameterized Constructor
The constructors that take some arguments are known as parameterized constructors.
Example:
class Student {
String name;
String surname;
int rollNo;
}
}
4. Copy Constructor
A copy constructor is a member function that initializes an object using another
object of the same class.
Example:
class Student {
this.name = student.name;
this.surname=student.surname;
this.rollNo= student.rollNo;
}
}
n Python, we do not have built-in copy constructors like Java and C++ but we can
make a workaround using different methods.
class base {
public:
class base {
void func()
}
}