Java

OOPS in Java

OOPS in Java

Object-oriented programming (OOP) is a software development paradigm that focuses on creating and manipulating objects in order to design and develop applications. OOP makes use of classes which are defined as blueprints for individual objects, each containing properties or data fields along with associated methods used to manipulate the object's characteristics. By reusing existing code through inheritance and abstraction, developers can create complex applications more quickly than using other paradigms like procedural programming. Additionally, OOP also offers scalability by supporting encapsulation which helps reduce complexity by providing restricted access to various elements within an application; polymorphism enabling multiple implementations of similar operations; finally message passing allowing communication between different components –all helping ensure reliable results backed up by robust safety measures. 

Inheritance: 
Inheritance is one of the core features of object-oriented programming with Java and it allows developers to create new classes that inherit properties from existing ones. This provides a mechanism for code reuse, as well as allows developers to model relationships between types in a more natural way. The concept of Inheritance is as the word says - There will be a child class and a parent class and the child class can inherit the properties from the parent class. 

The class that is inherited from is called the superclass or parent class, while the class that inherits is called the subclass or child class. A subclass can inherit properties and methods from its superclass, and can also add new properties and methods. 
Java supports single inheritance, where a class can inherit from only one superclass, but it also supports multiple inheritances through interfaces. 

Example: 

class Shape { 
int x; 
int y; 
public void changeShape(int x, int y) { 
this.x = x; 
this.y = y; 
} 
} 
class Circle extends Shape { 
int radius; 
public double getArea() { 
return Math.PI * radius * radius; 
} 
} 

In this example, the Circle class inherits from the Shape class. It inherits the x and y properties and the changeShape() method and also has its own radius property and getArea() method. 
Java also provides a way to prevent a subclass from overriding a method in the superclass by using the "final" keyword. 
For example, the following code prevents the Circle class from overriding the changeShape() method: 

class Shape { 
int x; 
int y; 
public final void changeShape(int x, int y) { 
this.x = x; 
this.y = y; 
} 
} 

Java also provides a way to access the properties and methods of the superclass using the "super" keyword. 
For example, the following code calls the changeShape() method of the superclass: 

class Circle extends Shape { 
int radius; 
public void changeCircle(int x, int y) { 
super.changeShape(x, y); 
} 
} 

Polymorphism 
Polymorphism is a mechanism that allows objects of different classes to be treated as objects of a common superclass or interface. This allows for a single method or variable to be used with multiple types of objects, making the code more flexible and reusable. 

Types of Polymorphism: 
● Compile-time polymorphism: Also known as static polymorphism, this type of polymorphism is achieved through method overloading. Method overloading allows a class to have multiple methods with the same name but different parameters. The correct method to be called is determined at compile-time based on the arguments passed to the method. 
Example: 

class Calculator { 
public int add(int x, int y) { 
return x + y; 
} 
public int add(int x, int y, int z) { 
return x + y + z; 
} 
} 

In this example, the Calculator class has two methods with the same name "add", but with different parameters. The correct method to be called is determined at compile-time based on the number of arguments passed to the method. 

Run-time polymorphism: Also known as dynamic polymorphism, this type of polymorphism is achieved through method overriding. Method overriding allows a subclass to provide a different implementation of a method that is already defined in its superclass. The correct method to be called is determined at runtime based on the actual type of the object. 

class Shape { 
public void draw() { 
System.out.println("Drawing a shape."); 
} 
} 
class Circle extends Shape { 
@Override 
public void draw() { 
System.out.println("Drawing a Circle."); 
} 
} 

In this example, the Shape class has a draw() method, and the Circle class extends the Shape class and overrides the draw() method. 

Shape s = new Circle(); 
s.draw(); 

When the draw() method is called on the s object, the JVM will determine at runtime that the actual type of the object is Circle, and it will call the draw() method of the Circle class. 
Java also provides a way to use polymorphism through interfaces. An interface defines a set of methods a class must implement, allowing objects of different classes to be treated as objects of a common interface. 

interface Printable { 
public void print(); 
} 
class Book implements Printable { 
public void print() { 
System.out.println("Printing a book."); 
} 
} 
class Magazine implements Printable { 
public void print() { 
System.out.println("Printing a magazine."); 
} 
} 

In this example, the Printable interface defines a print() method, and the Book and Magazine classes implement the Printable interface by providing their own specific implementation of the print() method. 

Printable p = new Book(); 
p.print(); 

Abstraction 
Abstraction is an essential concept in object-oriented programming with Java and it gives developers the ability to simplify complex operations by hiding away unnecessary details. This provides a mechanism for code reuse, as well as allows developers to focus on the purpose of each class instead of worrying about implementation specifics. Abstraction also helps reduce complexity by enabling users to group related operations into single entities – all helping ensure reliable results backed up by robust safety measures. 
Let us look into the concept of abstraction in detail: 

Abstract classes
An abstract class is a class that cannot be instantiated and is intended to be subclassed. An abstract class can have abstract methods, which are methods without a body, and are intended to be overridden by subclasses. An abstract class can also have concrete methods, which are methods with a body, and can be used by subclasses. 
Example: 

abstract class Shape { 
int x; 
int y; 
public abstract void draw(); 
public void changeShape(int x, int y) { 
this.x = x; 
this.y = y; 
} 
}

In the above example, the Shape class is an abstract class that has an abstract method draw() and a concrete method changeShape(). Subclasses of Shape must implement the draw() method, but can use the changeShape() method. 

Interfaces
An interface is a collection of abstract methods that a class can implement but does not provide an implementation for those methods. A class can implement multiple interfaces, allowing for a flexible and reusable design. 
Example: 

interface Printable { 
public void print(); 
} 
class Book implements Printable { 
public void print() { 
System. out.println("Printing a book."); 
} 
} 

In this example, the Printable interface defines a print() method and the Book class implements the Printable interface by providing its own specific implementation of the print() method. 
Abstract classes and interfaces are used to achieve abstraction in Java by creating a clear separation between the implementation details of an object and its interface. This allows for a simplified representation of an object, hiding the underlying complexity and making the code more flexible and reusable.

Conclusion 

So there you have it. You now know the basics of Java and OOPs. With this handy tutorial as your guide, you're well on your way to mastering this powerful programming language. Remember to practice and experiment with the concepts you've learned, and you'll be a Java pro in no time. 
Keep in mind that this is just a basic introduction to Java and OOPs. There's a lot more to learn if you want to become a true Java expert. But with this tutorial, you now have 
the foundation you need to get started on your journey. So what are you waiting for? Start coding! 

Top course recommendations for you

    Introduction to JavaScript
    3 hrs
    Beginner
    87.5K+ Learners
    4.45  (3789)
    Data Structure & Algorithms in Java for Intermediate Level
    4 hrs
    Intermediate
    14.4K+ Learners
    4.48  (1984)
    Building Games using Java
    2 hrs
    Beginner
    26K+ Learners
    4.28  (152)
    Algorithms in C
    3 hrs
    Beginner
    26.3K+ Learners
    4.44  (580)
    Angular7 for Beginners
    3 hrs
    Beginner
    19.1K+ Learners
    4.54  (651)
    Angular7 for Intermediate Level
    3 hrs
    Intermediate
    8.5K+ Learners
    4.59  (215)
    Introduction to Kubernetes
    2 hrs
    Beginner
    6.8K+ Learners
    4.29  (252)
    Angular7 for Advanced Level
    3 hrs
    Advanced
    9.6K+ Learners
    4.62  (229)
    Java Algorithms
    2 hrs
    Intermediate
    19.1K+ Learners
    4.49  (419)
    Visual Studio Online
    1 hrs
    Beginner
    13K+ Learners
    4.4  (463)