Docsity

Object-Oriented Programming Concepts in Java, Study Guides, Projects, Research of Computer science

Partial preview of the text

Download Object-Oriented Programming Concepts in Java and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

Java Institute for Advanced Technology

OBJECT ORIENTED PROGRAMMING

CONCEPTS

H7E3 04

H7E3 04/AS/

Naleesh Vishvanath Perera

973421441V

COLOMBO

Object Oriented Programming Object-oriented programming ( OOP ) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes), and code, in the form of procedures (often known as methods). A feature of objects is an object's procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another.[1][2]^ OOP languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.  History of Object Oriented Programming SIMULA was the first object language. As its name suggests it was used to create simulations. Alan Kay, who was at the University of Utah at the time, liked what he saw in the SIMULA language. He had a vision of a personal computer that would provide graphics-oriented applications and he felt that a language like SIMULA would provide a good way for non- specialists to create these applications. He sold his vision to Xerox Parc and in the early 1970s, a team headed by Alan Kay at Xerox Parc created the first personal computer called the Dynabook. Smalltalk was the object-oriented language developed for programming the Dynabook. It was a simulation and graphics-oriented programming language. Smalltalk exists to this day although it is not widely used commercially. The idea of object-oriented programming gained momentum in the 1970s and in the early 1980s Bjorn Stroustrup integrated object-oriented programming into the C language. The resulting language was called C++ and it became the first object-oriented language to be widely used commercially. In the early 1990s a group at Sun led by James Gosling developed a simpler version of C++ called Java that was meant to be a programming language for video-on-demand applications. This project was going nowhere until the group re-oriented its focus and marketed Java as a language for programming Internet applications. The language has gained widespread popularity as the Internet has boomed, although its market penetration has been limited by its inefficiency.

Refers to linking of function call with function defination is called binding and when it is take place at run time called dynamic binding. Message Passing The process by which one object can interact with other object is called message passing. Variables... A variable furnishes us with named capacity that our projects can control. Every variable in Java has a particular sort, which decides the size and design of the variable's memory; the scope of qualities that can be put away inside that memory; and the arrangement of operations that can be connected to the variable.

There’re 3 types of variables

Local variables  Local variables are declared in methods, constructors, or blocks.  Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.  Access modifiers cannot be used for local variables.  Local variables are visible only within the declared method, constructor, or block. Example Instance variables  Instance variables are declared in a class, but outside a method, constructor or any block.

 Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Example Class/Static variables  Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.  There would only be one copy of each class variable per class, regardless of how many objects are created from it. Example Design Patterns

 Private Class Data

Restricts accessor/mutator access

 Proxy

An object representing another object Behavioral design patterns

 Chain of responsibility

A way of passing a request between a chain of objects

 Command

Encapsulate a command request as an object

 Interpreter

A way to include language elements in a program

 Iterator

Sequentially access the elements of a collection

 Mediator

Defines simplified communication between classes

 Memento

Capture and restore an object's internal state

 Null Object

Designed to act as a default value of an object

 Observer

A way of notifying change to a number of classes

 State

Alter an object's behavior when its state changes

 Strategy

Encapsulates an algorithm inside a class

 Template method

Defer the exact steps of an algorithm to a subclass

 Visitor

Defines a new operation to a class without change IS-A and HAS-A Relationships

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 (class inheritance) keyword or implements (interface inheritance) keyword in a class declaration, then this class is said to have IS-A relationship. Has-A Relationship: These relationships are for the most part in light of the use. This decides if a specific class HAS-A specific thing. This relationship decreases duplication of code and in addition bugs interface Fruit {} class Mango implements Fruit{ } Mango IS-A Fruit class Animal {} class Mango extends Dog{ } Dog IS-A Animal

Overriding and Overloading

Overloading

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. Constructor overloading Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task. page 9

class A{

static int add(int a,int b) {

return a+b;}

static int add(int a,int b,int c) {

return a+b+c;}

static int add(String c) {

return 10; } }

Overloaded add Methods

class StudentData{

StudentData(){

StudentData(int num1, String str, int num2){

StudentData(String str, int num2){

Over Loaded StudentData

Constructors

Overriding Declaring a method in sub class which is already present in parent class is known as method overriding. Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.

class Human{

//Overridden method

public void eat(){

System.out.println("Human is eating");

class Boy extends Human{

//Overriding method

public void eat(){

System.out.println("Boy is eating");

public static void main( String args[]) {

Boy obj = new Boy();

//This will call the child class version of eat()

obj.eat();

OutPut: Boy is eating

Polymorphism Polymorphism is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding. If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java. Compile time polymorphism Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading is an example of compile time polymorphism. Example: page 12 class SimpleCalculator{ int add(int a, int b)} return a+b; } int add(int a, int b, int c){ return a+b+c; } }public class Demo{ public static void main(String args[]){ SimpleCalculator obj = new SimpleCalculator(); System.out.print(obj.add(10, 20)); System.out.print(obj.add(10, 20, 30)); } }

OutPut: 30 60

Runtime Polymorphism (or Dynamic polymorphism) It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, that’s why it is called runtime polymorphism.

class ABC{

public void myMethod(){

System.out.println("Overridden Method");

public class XYZ extends ABC{

public void myMethod(){

System.out.println("Overriding");

public static void main(String args[]){

ABC obj = new XYZ();

OutPut: Overriding

What is Encapsulation? Encapsulation is one of fundamental OOP concepts. it is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding. To achieve encapsulation in Java −  Declare the variables of a class as private.  Provide public setter and getter methods to modify and view the variables values. public class Student { private String name; public String getName(){ return name; } public void setName(){ this.name=name;}}