Oopc Complete 01
Oopc Complete 01
HAS-A RELATIONSHIP
o ASSOCIATION
o AGGREGATION
o COMPOSITION
o POLYMORPHISM
o ENCAPSULATION
o ABSTRACTION
CONCEPT
The Object-Oriented programming idea started in 1970s and early 1980s “Bjorn Striustrup”
integrated OOP into C language. After resulting language was called C++ and it became the
first OOP language to be widely used commercially. Early 1990s a group at Sun led by
“James Gosling” developed a simpler version of C++ called Java that was meant to be
programming language for video-on-demand applications. Re-oriented this project focused in
Java as a language for programming Internet applications.
o IS-A Relationship
o HAS-A relationship
Association
Aggregation
Composition
o Polymorphism
o Abstraction
o Encapsulation
o Association
o Interfaces
o Abstract class
IS-A RELATIONSHIP
The concept of IS-A relationship in object oriented programing concept totally based
on inheritance, which can be of two types class inheritance or interface inheritance. For
example to IS-A relation using inheritance like “Animal and cat” or “Fruit and mango”, Cat
is an animal but Animal is not a cat, Mango is a fruit but Fruit is not a mango. It’s just saying
“A is a B type of things”.
Code example
1. Class BMW {
Class Vehicles {
}}
class vehicles {
Code example
package relationships;
class Car {
// Methods implementation and class/Instance members
private String color;
private int maxSpeed;
public void carInfo(){
System.out.println("Car Color= "+color + " Max Speed= " + maxSpeed);
}
public void setColor(String color) {
this.color = color;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
This code example Key object is in to the Driver object and wants to access the Key object you
need to use the Driver object.
ASSOCIATION
Ex-:
o Multiple under graduates can associate with single lecturer and single under graduate
can associate with multiple lecturers, but there have no ownership between the objects
and both have their own lifecycle. Both can create and delete independently.
AGGREGATION
Code example
1. class Room {
class Rack {
class check{
public static void main(String args[]){
Room room = new Room();
Rack newrack = new rack();
}}
COMPOSITION
Code example
1. class Building {
Roof roof;
Public Building()
{ This.roof = new
Roof();
}}
class Roof {
Class Wood {
Static binding
Dynamic binding
Dynamic binding, also called virtual method invocation, is the binding used for
instance methods to allow for polymorphism. Dynamic binding, as opposed to static binding,
means that the binding of instance methods to the appropriate implementation is resolved at
runtime, based on the instance and its type.
ENCAPSULATION
Encapsulation is one of the four fundamental OOP concepts. The other three
are inheritance, polymorphism and abstraction. Encapsulation is the technique of making the
fields in a class private and providing access to the fields via public methods. If a field is
declared private, it can’t access anyone in outside of the class. For this reason, encapsulation
is also referred to as data hiding, data immutability and data validation.
Code example
1. class A {
private int balance = 2000;
private String name = “Kamal”;
}
class B {
public static void main(String args[]){
A a = new A();
}}
This code example “class A” is a private class, “class B” can’t create a new object to class A,
because class A is private.
Encapsulation helps to create code that is loosely coupled. Because details are hidden, it
reduces the ability of other objects to directly modify an object’s state and behavior.
Encapsulation can,
Overriding
1. Overriding happens at runtime. The binding of overridden method call to it’s happens
at runtime.
2. Static methods cannot be overridden, even declare a same static method in child class
it has nothing to do with the same method of parent class.
3. Dynamic binding is being used for overriding methods.
4. The reason is that the binding of overridden methods is being done at runtime.
Overloading
5. Private and final methods can be overloaded but they cannot be overridden.
7. Return type of method does not matter in case of method overloading, it can be same
or different.
Code example for method Overriding
class animal{
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
ABSTRACTION
Abstract classes
1. Abstract class can have abstract and non-abstract methods (implemented methods).
3. Abstract class can have static, non-static, final and non-final variables.
4. Abstract class can have static methods, main method and constructor.
BOOK NAME
CHAPTER
ARTICLE
Polymorphism, Page-244
Interfaces, Page-252
AUTHORS