









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










SANJULA BIYANWILA
977671205V COLOMBO
Overview In the class-based object-oriented programming paradigm, “ object ” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures. A good understanding of OOPs concepts can help in decision making when designing an application. How you should design an application and what language should be used. Object Oriented programming is a programming style which is associated with the concepts like class , object, Inheritance , Encapsulation , Abstraction , Polymorphism. Most popular programming languages like Java, C++, C#, Ruby, etc. follow an object oriented programming paradigm.
CLASS: Class is a collection of objects of similar type. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. DATA ABSTRACTION AND ENCAPSULATION: Combining data and functions into a single unit called class and the process is known as Encapsulation. Data encapsulation is important feature of a class. Class contains both data and functions. Data is not accessible from the outside world and only those function, which are present in the class, can access the data. The insulation of the data from direct access by the program is called data hiding or information hiding. Hiding the complexity of program is called Abstraction and only essential features are,we can say that internal working is hidden. INHERITANCE: it is the process by which object of one class acquire the properties or features of objects of another class. The concept of inheritance provide the idea of reusability means we can add additional features to an existing class without modifying it. This is possible by driving a new class from the existing one. The new class will have the combined features of both the classes. POLYMORPHISM: A Greek term means ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation.
IS-A Relationship: In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House.
It is a key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS- A relationship. HAS-A Relationship: Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom. 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; } }
myMaruti.carInfo(); myMaruti.MarutiStartDemo(); } } Q: What is Polymorphism?? Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object. A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type. Run Time polymorphism public class Animal{ public void sound(){ System.out.println("Animal is making a sound");
class Horse extends Animal{ @Override public void sound(){ System.out.println("Neigh"); } public static void main(String args[]){ Animal obj = new Horse(); obj.sound(); } } output - Neigh public class Cat extends Animal{ @Override public void sound(){ System.out.println("Meow"); } public static void main(String args[]){ Animal obj = new Cat(); obj.sound(); } } output - Meow Compile Time polymorphism Method Overloading on the other hand is a compile time polymorphism example. class Overload
Overriding Overloading Method overloading is used to increase the readability of the program Method overriding is used to provide the specific implementation of the method that is already provided by its super class. Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship In case of method overloading, parameter must be different. In case of method overriding, parameter must be same. Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism. In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Return type must be same or covariant in method overriding. class OverloadingExample{ static int add(int a,int b){ return a+b; } static int add(int a,int b,int c){ return a+b+c; }
Public static void main(String args[]){ System.out.println(OverloadingExample.add(10,20)); System.out.println(OverloadingExample.add(30,40,50)); } } Output – 30 120 class Animal{ void eat(){ System.out.println("eating..."); } } class Dog extends Animal{ void eat(){ System.out.println("eating bread..."); } } class Test{ public static void main(String args[]){ System.out.println(new Dog().eat()); } } Output - eating bread…
public void setAge( int newAge) { age = newAge; } public void setName(String newName) { name = newName; } public void setIdNum( String newId) { idNum = newId; } } The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any class that wants to access the variables should access them through these getters and setters. The variables of the EncapTest class can be accessed using the following program − public class RunEncap { public static void main(String args[]) { EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); } } This will produce the following result −
Output - Name : James Age : 20
Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces. Abstraction is one of the four major concepts behind object-oriented programming (OOP). public abstract class Employee { private String name; private int paymentPerHour; public Employee(String name, int paymentPerHour) { this.name = name; this.paymentPerHour = paymentPerHour; } public abstract int calculateSalary(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPaymentPerHour() { return paymentPerHour; } public void setPaymentPerHour(int paymentPerHour) { this.paymentPerHour = paymentPerHour;