What is OOP

OOP Ideas

Contents

Blue print of the objects

An object is an instance of a class (Class -> blueprint, Object -> house)

Initialize the state of an object

Automatically called at the end of lifetime of an object Free up the acuired resources

Inheritance (Parent-Child)

class Apple extends Fruit {
}

Foo is-a Bar:

public class Foo extends Bar{}

Composition: Creating instances which have references to other objects (Objects depend on each other)

class Room {
Table table = new Table();
}

Foo has-a Bar:

public class Foo {
  private Bar bar;
}

Has-A(Own Life Time | Exists Independently/Isolation) eg. Manager has a Swipe Card

Has-A(Ownership | Single owner of Child Object | Child Objects can exists Independently) eg. One Manager - Many Workers

Exposing only the relevant details of an entity

A class can extend multiple abstract classes

Binding data and operations together in an entity

  • Information hiding: Access control modifier
  • Implementation hiding: Through creation of interface for class

Abstraction: The concept of allowing the user of your class to have access to only what they need. (Concept)

Encapsulation: The physical code that prevents the user from accessing fields or methods you do not want them to (Actual Implementation)

  • Reusability of code
  • Inheriting characteristics from parent class to child class without modifications

An object can have different forms

  • Static polymorphism: method overloading
  • Dynamic polymorphism: method overriding

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
abstract class Animal {
	String name;

	//cool functionality
	abstract String bark();
}

// Inheritance
class Dog extends Animal {
	String bark() {
		return "Bow Bow";
	}
}

class Cat extends Animal {
	String bark() {
		return "Meow Meow";
	}
}

public class InheritanceExamples {
	public static void main(String[] args) {
		Animal animal = new Cat();
		System.out.println(animal.bark());
	}
}

Overloading: Same name but different parameters

Overriding: Same name and same parameters

/images/overloading-vs-overriding.png

Abstract class is a half-defined parent class. Needs to be extened and methods implemented

Can’t be instantiated

User is not told about the process but only the method about the class (single approach)

public abstract class Customer
{
    public abstract decimal Discount();
}

public class richCustomer : Customer
{
    public override decimal Discount();
}

A class with no implementation

It contains only the declaration of methods, parameters and values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface Flyable {
	void fly();
}

class Aeroplane implements Flyable {
	public void fly() {
		System.out.println("Aeroplane is flying");
	}
}

class Bird implements Flyable {
	public void fly() {
		System.out.println("Bird is flying");
	}
}

public class InterfaceExamples {
	public static void main(String[] args) {
		Flyable flyable = new Bird();
		flyable.fly();
	}
}

/images/interface-vs-class.png Fist interface created, then abstract class, then concrete classes

/images/interface-vs-class2.png

/images/binding.png

Powered By Valine
v1.4.14