Sunday, June 17, 2012

What are the core OOP’s concepts?

  • ChatGPT

2. Interface

Definition: An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields. The methods in interfaces are abstract by default.

Purpose:

    Define Contracts: Interfaces define a contract that implementing classes must follow.
    Multiple Inheritance: Interfaces allow a class to implement multiple interfaces, overcoming the limitation of single inheritance in Java.

Example in Java:

// Interface
interface Drawable {
    void draw();
}

// Class implementing the interface
class Rectangle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

class Test {
    public static void main(String[] args) {
        Drawable myRectangle = new Rectangle();
        myRectangle.draw(); // Outputs: Drawing a rectangle
    }
}


In this example, Drawable is an interface with an abstract method draw(). The Rectangle class implements the Drawable interface and provides its own implementation of draw(). Interfaces define a contract that the Rectangle class adheres to.

1. Abstraction

Definition: Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It focuses on what an object does rather than how it does it.

Purpose:

    Simplify Complex Systems: By focusing on essential characteristics, abstraction simplifies interaction with complex systems.
    Define Common Interface: Allows you to define a common interface for different implementations.

// Abstract class
abstract class Animal {
    // Abstract method (does not have a body)
    public abstract void makeSound();

    // Regular method
    public void sleep() {
        System.out.println("This animal sleeps");
    }
}

// Subclass providing specific implementation
class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

class Test {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.makeSound(); // Outputs: Bark
        myDog.sleep();     // Outputs: This animal sleeps
    }
}


In this example, Animal is an abstract class with an abstract method makeSound(). The Dog class provides a specific implementation of makeSound(). Abstraction allows us to use the Animal class as a common interface without knowing the details of how makeSound() is implemented.

1. Encapsulation

Definition: Encapsulation is the concept of wrapping data (attributes) and methods (functions) that operate on the data into a single unit, usually a class. This means hiding the internal state of an object and requiring all interactions to be performed through an object’s methods.

Purpose:

    Protection: It protects an object’s internal state from unintended or harmful modifications.
    Controlled Access: By providing public methods (getters and setters), encapsulation allows controlled access to the internal data.
    Flexibility: Changes to the internal implementation of a class can be made with minimal impact on other parts of the code.
Example in Java:

public class Person {
    private String name; // private attribute

    // Constructor
    public Person(String name) {
        this.name = name;
    }

    // Public method to access private attribute
    public String getName() {
        return name;
    }

    // Public method to modify private attribute
    public void setName(String name) {
        this.name = name;
    }
}


2. Polymorphism

Definition: Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The most common use of polymorphism is method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

Purpose:

    Flexibility: Allows objects of different classes to be treated through a common interface.
    Method Overriding: Enables a subclass to provide a specific implementation of a method that is already defined in its superclass.

Example in Java:

class Animal {
    public void makeSound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.makeSound(); // Outputs: Bark
        myCat.makeSound(); // Outputs: Meow
    }
}


3. Inheritance

Definition: Inheritance is a mechanism where one class (subclass or derived class) inherits attributes and methods from another class (superclass or base class). This promotes code reusability and establishes a natural hierarchy between classes.

Purpose:

    Reusability: Allows a subclass to reuse code from the superclass, reducing redundancy.
    Hierarchy: Establishes a natural hierarchical relationship between classes.
    Extensibility: Facilitates the creation of new classes based on existing ones, adding or modifying functionality as needed.

Example in Java:

class Animal {
    public void eat() {
        System.out.println("This animal eats food");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println("Dog barks");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat(); // Inherited method
        myDog.bark(); // Subclass-specific method
    }
}

Summary:

    Encapsulation: Bundles data and methods, restricting direct access to some of the object's components.
    Polymorphism: Allows objects to be treated as instances of their parent class, enabling method overriding and flexibility.
    Inheritance: Enables a new class to inherit attributes and methods from an existing class, fostering code reuse and hierarchy


  • 1. Encapsulation

  • Definition: Encapsulation is the concept of wrapping data (attributes) and methods (functions) that operate on the data into a single unit, usually a class. This means hiding the internal state of an object and requiring all interactions to be performed through an object’s methods.

  • Purpose:

  •     Protection: It protects an object’s internal state from unintended or harmful modifications.
  •     Controlled Access: By providing public methods (getters and setters), encapsulation allows controlled access to the internal data.
  •     Flexibility: Changes to the internal implementation of a class can be made with minimal impact on other parts of the code.
  • Example in Java:

  • public class Person {
  •     private String name; // private attribute

  •     // Constructor
  •     public Person(String name) {
  •         this.name = name;
  •     }

  •     // Public method to access private attribute
  •     public String getName() {
  •         return name;
  •     }

  •     // Public method to modify private attribute
  •     public void setName(String name) {
  •         this.name = name;
  •     }
  • }


  • 2. Polymorphism

  • Definition: Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The most common use of polymorphism is method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

  • Purpose:

  •     Flexibility: Allows objects of different classes to be treated through a common interface.
  •     Method Overriding: Enables a subclass to provide a specific implementation of a method that is already defined in its superclass.

  • Example in Java:

  • class Animal {
  •     public void makeSound() {
  •         System.out.println("Some sound");
  •     }
  • }

  • class Dog extends Animal {
  •     @Override
  •     public void makeSound() {
  •         System.out.println("Bark");
  •     }
  • }

  • class Cat extends Animal {
  •     @Override
  •     public void makeSound() {
  •         System.out.println("Meow");
  •     }
  • }

  • public class Test {
  •     public static void main(String[] args) {
  •         Animal myDog = new Dog();
  •         Animal myCat = new Cat();

  •         myDog.makeSound(); // Outputs: Bark
  •         myCat.makeSound(); // Outputs: Meow
  •     }
  • }


  • 3. Inheritance

  • Definition: Inheritance is a mechanism where one class (subclass or derived class) inherits attributes and methods from another class (superclass or base class). This promotes code reusability and establishes a natural hierarchy between classes.

  • Purpose:

  •     Reusability: Allows a subclass to reuse code from the superclass, reducing redundancy.
  •     Hierarchy: Establishes a natural hierarchical relationship between classes.
  •     Extensibility: Facilitates the creation of new classes based on existing ones, adding or modifying functionality as needed.

  • Example in Java:

  • class Animal {
  •     public void eat() {
  •         System.out.println("This animal eats food");
  •     }
  • }

  • class Dog extends Animal {
  •     public void bark() {
  •         System.out.println("Dog barks");
  •     }
  • }

  • public class Test {
  •     public static void main(String[] args) {
  •         Dog myDog = new Dog();
  •         myDog.eat(); // Inherited method
  •         myDog.bark(); // Subclass-specific method
  •     }
  • }

  • Summary:

  •     Encapsulation: Bundles data and methods, restricting direct access to some of the object's components.
  •     Polymorphism: Allows objects to be treated as instances of their parent class, enabling method overriding and flexibility.
  •     Inheritance: Enables a new class to inherit attributes and methods from an existing class, fostering code reuse and hierarchy
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism



Polymorphism, Inheritance and Encapsulation

Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to

make once and have effect in all relevant places

Explain the Polymorphism principle. Explain the different forms of Polymorphism.
Polymorphism in simple terms means one name many forms.
Polymorphism exists in three distinct forms in Java:
• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface

method overloading is the primary way polymorphism is implemented in Java

overloaded methods:
appear in the same class or a subclass
have the same name but,
have different parameter lists, and,
can have different return types

an example of an overloaded method is print() in the java.io.PrintStream class
public void print(boolean b)
public void print(char c)
public void print(char[] s)
public void print(float f)
public void print(double d)
public void print(int i)
public void print(long l)
public void print(Object obj)
public void print(String s)



Overriding methods
appear in subclasses
have the same name as a superclass method
have the same parameter list as a superclass method
have the same return type as as a superclass method
the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method



Explain Encapsulation, Polymorphism and Inheritance.
http://www.youtube.com/watch?v=QzX7REqciPY




  • Inheritance - comes under Relationship -> Generalization/Specilization -> enables code resuability. But also enables maintanence nightmare of managing classes.

Polymorphism - comes unders Behaviour->Compile time/Runtime -> enables the varying behaviour of the operations depending upon the type of the object(runtime entity).

http://www.linkedin.com/groups/what-is-difference-between-dynamic-70526.S.217712129?view=&srchtype=discussedNews&gid=70526&item=217712129&type=member&trk=eml-anet_dig-b_pd-ttl-cn&ut=23CVDF6gnvwlE1

  • Class versus object
A class is a template for objects. A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.
https://www.ncl.ucar.edu/Document/HLUs/User_Guide/classes/classoview.shtml#:~:text=A%20class%20defines%20object%20properties,are%20defined%20by%20default%20settings.

  • Let's see some real life example of class and object in java to understand the difference well:

Class: Human Object: Man, Woman

Class: Fruit Object: Apple, Banana, Mango, Guava wtc.

Class: Mobile phone Object: iPhone, Samsung, Moto

Class: Food Object: Pizza, Burger, Samosa

https://www.javatpoint.com/difference-between-object-and-class

No comments:

Post a Comment