Overview of Object Oriented Programming Language(OOP's)


  • Encapsulation is one of the fundamental Oops concepts.
  • Encapsulation is nothing but hiding the data.(i.e) the data which we have wriiten does not access by other persons .
  • In Encapsulation never keep fields access modifier is “public”. Because if the fields are public we lose flexibility, maintainability of the class. How means

Example program:

// The following program shows what are the problems may occur…

public class Encapsulation { public int size; public int weight; } public class RunEncaps { public static void main(String[] args) { Encapsulation e = new Encapsulation(); e.size = -5; System.out.println("size is "+e.size); } }

In the above example we take size as int type and access modifier is public. when somebody changes the value the value of size will be change. So we can handle this problem by using the “Encapsulation”.

  • In Encapsulation fields must be “private” access modifier. That field cannot be accessed by anyone outside the class, there by hiding the fields for the class.
  • If we are following the JavaBeans standards (getter & setter methods) it achieves the “Encapsulation”.
  • The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this nature Encapsulation allows the user utilises flexibility and extensibility to our code.
  • The fields present in a class can be made read purpose only.
  • A class will have the entire control on the fields which are stored in that particular class.
  • The users of a class do not know how class stores its data.
  • A class can change data type of a field, and users of the class do not need to change of their data.
public class Encapsulaion { private String name; private int idNum; private int age; public int getAge(){ return age; } public String getName() { return name; } public int getIdNum() { return idNum; } public void setAge ( int newAge){ age = newAge; } public void setName (String newName){ name = newName; } public void setIdNum( int newId){ idNum = newId; } } public class RunEncap { public static void main(String args[]){ Encapsulaion encap = new Encapsulaion(); encap.setName("James"); encap.setAge(20); encap.setIdNum(123); System.out.print("Name : " + encap.getName()); System.out.print("Age: " + encap.getAge()); System.out.print("Id : " + encap.getIdNum()); } }


  • Inheritance is nothing but a process by which one class can acquires the properties of another class.
  • The main theme of inheritance is reusability. Reusability means a class can use the properties of existing class without modifying it.
  • For this reason we can create a new class which accesses the properties of existing class. This new class is called “subclass or child class” and existing class is called “super class or parent class”.
  • A subclass can use the properties of parent class without modifying the parent class is called “Inheritance”.

extends keyword:

The keyword “extends” signifies that the properties of super class are extended to the sub class. That means, subclass contains its own members as well of those of super class. This kind of situation occurs when we want to enhance the properties of existing class without actually modifying it.
public class SubClassName extends SuperClasName { fields declaration; methods declaration; }

There are 5 types of inheritance

  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hybrid inheritance
  5. Hierarchal inheritance

i) Single inheritance:-

Single Inheritance will acquires the properties of one class (parent ) into another class (child).
(B is the subclass, A is the super class)

Example program:

class Dimensions { public int length; public int breadth; } class Rectangle2 extends Dimensions { int a; void area(){ a = leangth * bradth; } } public class Area1{ public static void main(String[] args) { Rectangle2 rect = new rectangle2(); rect.length = 13; rect.breadth = 15; rect.area(); System.out.println("The Area of rectangle of length+ rect.leangth +and bradth"" +rect.bradth +"is"+rect.a); } }

ii) Multiple inheritance:

In multiple inheritance, one class extends more than one class (multiple classes)

Java does not support multiple inheritance because if there are two parents for one child class then the JVM could not knows that which parent class to take. The following program does not work.


public class A { ------- ------- } public class B { --------- --------- public class C extends A,B { --------- --------- }

In the above code, C extends both A & B. This is not supported by Java and the above code raises compile time error Following is the schematic representation.

(C is The subclass,A,B are (many) superclasses)

iii) Multilevel inheritance:

In multilevel , one-to-one chain inheritance will grows. Multiple classes are included in inheritance, but one class extends only one class. The lowermost subclass can make use of all its super classes members.

(one parent , one subclass and chain continues)

Example program:

//program for multilevel inheritance
class Name2 { String name = "James"; int age = 20; } class Marks3 extends Name2 { int m1 = 3, m2 = 50, m3 = 50; } class Student3 extends Marks3 { int total; void calc() { total = m1 + m2 + m3; } void display() { System.out.println("NAME:"+name+"\nAGE:"+age+"\nM1=" +m1+"\nM2="+m2+"\nM3="+m3+"\nTOTAL:"+total); } } public class Area1 { public static void main(String[] args) { Student3 s = new Student3(); s.calc(); s.display(); } }


In hierarchical type of inheritance, one class will extended by more no of subclasses. It is one-to-many relationship.

(one parent class, many sub classes)

Example program:-

// program for Hierarchical Inheritance

public class A { ----------- ------------ } public class B extends A { ----------- ----------- } public class c extends B { ---------- ---------- } public class D { public static void main(String args[]) { --------- --------- } }

Is-A Vs Has-A


  • Is-A relationship is based on class inheritance or interface implementation.
  • Is-A is a way of saying “ this is a type of that thing “.i.e., child class extend parent class.
  • Is-A relationship in Java throws the keyword extends. (class inheritance) implements (interface implementation).
  • Is-A relationship works in only one directional.
Ex: House is-a building
public class House { ------------- ------------ }
public class Building extends House { Is_A_relationship
------------ ------------ }


  • Has-A relationships are implemented on usage rather than inheritance
  • In other words class A Has-A class B if code in class A has reference to an instance of Class B
public class Student { String name; int id; public Address address; } public class Address { String area; String mdl; String dist; public Student student; } }

In the above example Student and Address are related but not through inheritance. These two joined by using Has-A relationship. Student has-a reference to Address, but Student does not extend Address & vice versa.

  • Has-A relationship works in both ways (Bi-directional).

Method overloading:

  • In method overloading two or more methods will have the identical name but with different parameters.
  • Method overloading is used when objects are used to assign similar tasks but by using different input parameters.
  • When a method by an object is called, Java matches up the method name first and then the number and data type of parameters to decide which one of the overloaded methods to execute. i.e., compiler decides which method is to be called depending upon the number of parameters.
  • Access modifier & Non access modifier can also be changed.
  • Return type can also be changed.


//program for method overloading.

class OverLoading { public void display() { System.out.println("no parameters"); } public void display(int x) { System.out.println("value of x is "+x); } public void display(int x , int y) { System.out.println("values of x" +x+ "value of y"+y); } } public class OverLoadMain { public static void main(String[] args) { OverLoading ol = new OverLoading(); ol.display(); ol.display(10); ol.display(20, 30); } }

In the above program display() method is overloaded a number of times with different parameters.

When to use method overloading:

We must use this concept, when the same methods do different tasks. i.e., don’t use overloading if the multiple methods perform different tasks.

Method overriding:

  • In method overriding two or more methods have the same name same parameters with same data type.
  • A subclass will override the method by facilitating its own functionality to it ( by using @override)
  • Access modifier should be same or increases the scope of the access modifier.
  • Non access modifier – final :if a method can contain final keyword in a parent class we cannot override. static:if a method can contain static keyword child cannot override parent class methods but hide (child).
  • return type should be same or covariant type.


//program for method overriding

class MethodOverriding { public void display(int x) { System.out.println("Geek1"); } } class MethodOveriding1 extends MethodOverriding { public void display() { System.out.println("Geek2"); } } public class MainOverriding { public static void main(String[] args) { MethodOveriding1 r1 = new MethodOveriding1(); r1.display(); } }

Constructor overloading:

Like methods constructors can also be overloaded. Appropriate constructor is called as per the parameters which we pass at the time of object creation.

In the following program, the constructor Rectangle () is overloaded 2 times. Compiler differentiates which constructor is to be called depending on the number of parameters and their sequence of data types.


public class Rectangle { public Rectangle() { System.out.println("from default constructor"); } public Rectangle (int x) { System.out.println("area with one parameter"+x*x); } public static void main(String[] args) { System.out.println("overloading constructors"); Rectangle rect1 = new Rectangle(); Rectangle rect2 = new Rectangle(5); } }

Method Overloading Vs MethodOverriding

diff between method overload and override


  • Polymorphism is another important Oops concept. It means “there is a possible of wriiting in various forms more than one form”. Ex:An operation may have various nature in various instances. It depends up on the usage in the operation.“+”- it is used for adding two numbers and as well as concatenate the two strings.
  • Polymorphism is nothing but a “Dynamic method binding”.
  • In this the methods are binded at Runtime (Dynamic binding).
  • Fields are binded at compile time (static binding).
  • In polymorphism instance is referring to super class only.


// example program for polymorphism or dynamic binding method.

class Animal { public void sleep() { System.out.println("sleep"); } } class Dog extends Animal { public void bark() { System.out.println("bark"); } } class Monkey extends Animal { public void jump() { System.out.println("jump"); } } public class Polymorphism { public static void main(String[] args) { Animal a = new Dog(); Dog d = (Dog) a; // casting Animal type d.bark(); Animal b = new Monkey(); Monkey m = (Monkey)b; m.jump(); } }

Reference :

  • Every variable can have only one type.(Pizza type or Animal type etc.,)
  • Reference variable refer to object , it can change its reference to another object.
  • Ex:

    Pizza p = new Pizza();
    Here Pizza is reference type and p is reference variable.
  • First time variable is refer to one object after that it can refer to another object also.
  • If we put “final” in reference type then the reference variable can hold only one value and that can’t be changed.
  • Compiler always checks that which type of the variables are used.
    Left side variable type = Right side variable type

    Ex: -

    Pizza p = new Student(); // compile time error because type is different Pizza p = new Pizza(); // execute because type is same.


  • instanceof is a keyword.
  • instanceof is used to check to see if an object can be cast in to a specified type. The operands of instanceof are left operand is an object reference and right operand is class/interface.


// program for instanceof.

class Pizza { private double cost ; public void makePizza() { System.out.println("pizza"); } public double getCost() { return cost; } public void setCost(double cost) { this.cost = cost; } } class VegPizza extends Pizza { private double discount; public double getDiscount() { return discount; } public void setDiscount(int discount) { this.discount = discount; } public VegPizza(int discount) { this.discount = discount; } } class NonVegPizza extends Pizza { private double discount; public double getDiscount() { return discount; } public void setDiscount(double discount) { this.discount = discount; } public NonVegPizza(int discount) { this.discount = discount; } } class DiscountCalculator { double discountAmount; public double calDiscount(Pizza p) { if(p instanceof VegPizza) { VegPizza vp = (VegPizza)p; double d = vp.getDiscount(); System.out.println(d); double cost = vp.getCost(); double discountAmount = (cost*d)/100; return discountAmount; } else if (p instanceof NonVegPizza) { NonVegPizza np = (NonVegPizza)p; double d = np.getDiscount(); System.out.println(d); double cost1 = np.getCost(); double discountAmount = (cost1*d)/100; return discountAmount; } return 0; } } public class PizzaMain { public static void main(String[] args) { DiscountCalculator dis = new DiscountCalculator(); Pizza veg = new VegPizza(15); veg.setCost(75); sdouble cost = dis.calDiscount(veg); System.out.println(" discount amount is "+cost); Pizza nveg = new NonVegPizza(10); nveg.setCost(125); double cost1 = dis.calDiscount(nveg); System.out.println(" discount amount is "+cost1); } }

Type casting:

  • Casting occurs only when there is parent & child relationship exists.
  • When child class properties/methods used by parent class instance variable then casting should be used to access the child class properties.


// program for Typecasting

class Pizza { public void makePizza() { System.out.println("prepare pizza"); } } class VegPizza extends Pizza { private double discount; public double getDiscount() { System.out.println(discount); return discount; } public void setDiscount(double discount) { this.discount = discount; } } public class CastingExample { public static void main(String[] args) { Pizza p = new VegPizza(); p.makePizza(); VegPizza vp = (VegPizza)p; vp.setDiscount(20); vp.getDiscount(); } }
Casting primitive data types:

Converting one primitive data type to another data type is done by the following two ways:
Narrowing and Widening.


It is nothing but converting higher data type value into lower data type value. It is also called Explicit castng.

This explicit casting should be done by the programmer itself.

Ex: int a = 32; char ch = (char)a;

here we are converting int type value ‘a’ into char ch type by writing char in simple braces.

Widening:- It is nothing but converting lower data type value into higher data type value.

It is also called implicit casting.

It is an automatic casting done by the java compiler internally.

Ex: char ch = ‘A’; int num = (int)ch;

here we are converting the character ch into int type. For this we are using cast operator int written in simple braces before ch. Now the character value is converted into int and it is assigned to the value num.

HTML Comment Box is loading comments...