OOPS CONCEPTS
Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:
- class
- object
- inheritance
- polymorphism
- abstraction
- encapsulation
class
A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
example
public class Main {
int x = 5;
}
object
Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.
example
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
types of inheritance
types of inheritance
single level inheritance
When a class inherits another class, it is known as a single inheritance
example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
multi level inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
hierarchical inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance.
example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}}
- In java programming, multiple and hybrid inheritance is supported through interface only.
abstraction
Abstraction is a process of hiding the implementation details and showing only functionality to the user. There are two ways to achieve abstraction in java
- Abstract class
- interface
Abstract class
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Example
abstract class Animal{
public abstract void sound();
}
public class Dog extends Animal{
public void sound(){
System.out.println("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}
Output:
Woofinterface
An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
Example
interface AnimalEat {
void eat();
}
interface AnimalTravel {
void travel();
}
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
System.out.println("Animal is eating");
}
public void travel() {
System.out.println("Animal is travelling");
}
}
public class Main {
public static void main(String args[]) {
Animal a = new Animal();
a.eat();
a.travel();
}
}
polymorphism
Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.
In Java polymorphism is mainly divided into two types:
- Compile-time Polymorphism
- Runtime Polymorphism
Compile-time Polymorphism
It is also known as static polymorphism. Method Overloading: When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.
example
class Mo {
static int Multiply(int a, int b)
{
return a * b;
}
static int Multiply(int a, int b, int c)
{
return a * b * c;
}
}
class Test{
public static void main(String[] args)
{
System.out.println(Helper.Multiply(1, 2));
System.out.println(Helper.Multiply(2, 3, 4));
}
}
Runtime Polymorphism
It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
example
class Mo {
void Print()
{
System.out.println("parent class");
}
}
class subclass1 extends Parent {
void Print() { System.out.println("subclass1"); }
}
class subclass2 extends Parent {
void Print()
{
System.out.println("subclass2");
}
}
class test{
public static void main(String[] args)
{
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
o/p:-
subclass1subclass2
---------- or---------
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog{
public static void main(String args[]){
Animal b = new Dog(); // Animal reference but Dog object
b.move(); //Runs the method in Dog class
}
}
o/p:-
Animals can moveDogs can walk and run
encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
0 Comments
Got questions? Feel free to ask!