OOPs is an important key of programming language. We have to make our mind clear towards OOPs by understanding the each and every point given in this post.
- Single Inheritance: When a class inherits from only one superclass.
class ParentClass {
public void parentMethod() {
System.out.println("This is a parent method");
}
}
class ChildClass extends ParentClass {
public void childMethod() {
System.out.println("This is a child method");
}
}
// Creating an object of the child class
ChildClass obj = new ChildClass();
obj.parentMethod(); // Accessing parent method
obj.childMethod(); // Accessing child method
- Multiple Inheritance: Java doesn't support multiple inheritance of classes. However, it can be achieved using interfaces, which allow a class to implement multiple interfaces.
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
class MyClass implements Interface1, Interface2 {
public void method1() {
System.out.println("Method 1");
}
public void method2() {
System.out.println("Method 2");
}
}
// Creating an object of the class
MyClass obj = new MyClass();
obj.method1(); // Accessing method from Interface1
obj.method2(); // Accessing method from Interface2
- Hierarchical Inheritance: When multiple classes inherit from a single superclass.
class ParentClass {
public void parentMethod() {System.out.println("Parent Method");
}
}
class ChildClass1 extends ParentClass {
public void childMethod1() {
System.out.println("Child 1 Method");
}
}
class ChildClass2 extends ParentClass {
public void childMethod2() {
System.out.println("Child 2 Method");
}
// Creating objects of the child classes
ChildClass1 obj1 = new ChildClass1();
ChildClass2 obj2 = new ChildClass2();
obj1.parentMethod(); // Accessing parent method from ChildClass1
obj2.parentMethod(); // Accessing parent method from ChildClass2
obj1.childMethod1(); // Accessing method from ChildClass1
obj2.childMethod2(); // Accessing method from ChildClass2
Polymorphism
is the ability of objects to take on different forms based on the context. It
allows objects of different classes to be treated as objects of a common
superclass. Polymorphism can be achieved through method overriding or method
overloading.
- Method Overriding: When a subclass defines a method with the same name as a method in the superclass, it is said to override the superclass's method.
class
ParentClass {
public void showMessage() {
System.out.println("Parent class
message");
}
}
class
ChildClass extends ParentClass {
public void showMessage() {
System.out.println("Child class
message");
}
}
// Creating
objects of both classes
ParentClass
parentObj = new ParentClass();
ChildClass
childObj = new ChildClass();
parentObj.showMessage(); // Accessing parent class method
childObj.showMessage(); // Accessing child class method
- Method Overloading: When a class has multiple methods with the same name but different parameters, it is said to have overloaded methods.
class
MyClass {
public int sumNumbers(int a, int b) {
return a + b;
}
public int sumNumbers(int a, int b, int c) {
return a + b + c;
}
}
// Creating
an object of the class
MyClass obj
= new MyClass();
System.out.println(obj.sumNumbers(1,
2)); // Calling with two arguments
System.out.println(obj.sumNumbers(1,
2, 3)); // Calling with three arguments
Abstraction is the process of hiding unnecessary implementation details and exposing only essential features to the user. In Java, abstraction can be achieved using abstract classes or interfaces.
abstract
class AbstractClass {
public abstract void abstractMethod();
}
class
ConcreteClass extends AbstractClass {
public void abstractMethod() {
System.out.println("Implementation
of abstract method");
}
}
// Creating
an object of the concrete class
ConcreteClass
obj = new ConcreteClass();
obj.abstractMethod(); // Accessing the abstract method
implementation
Comments
Post a Comment