Docsity

Object-Oriented Programming (OOP) Concepts in Java, Schemes and Mind Maps of Computer science

Toggle sidebar
bg1
Contents
Overview..................................................................................................................................................... 3
History......................................................................................................................................................... 3
Object oriented programing........................................................................................................................ 3
Object......................................................................................................................................................3
Classes..................................................................................................................................................... 3
Variables.................................................................................................................................................. 3
There’re 3 types of variables............................................................................................................... 4
Inheritance..............................................................................................................................................6
1 | P a g e
Student Name R.A.R.R.Ramanayake
SQA ID Number 187328284
NIC No 977770963V
Subject Name OBJECT ORIENTED PROGRAMMING CONCEPTS - I
Subject Code H7DY 04
Assignment ID H7DY 04/AS/01
Branch Colombo
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Object-Oriented Programming (OOP) Concepts in Java and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Contents

Overview..................................................................................................................................................... 3 History......................................................................................................................................................... 3 Object oriented programing........................................................................................................................ 3 Object...................................................................................................................................................... 3 Classes..................................................................................................................................................... 3 Variables.................................................................................................................................................. 3 There’re 3 types of variables............................................................................................................... 4 Inheritance.............................................................................................................................................. 6

Student Name R.A.R.R.Ramanayake

SQA ID Number^187328284

NIC No 977770963V

Subject Name OBJECT ORIENTED PROGRAMMING CONCEPTS - I

Subject Code H7DY 04

Assignment ID H7DY 04/AS/

Branch Colombo

 - IS-A Relationship.................................................................................................................................. - HAS-A Relationship.............................................................................................................................. 
  • Polymorphism.........................................................................................................................................
  • Abstraction..............................................................................................................................................
  • Association..............................................................................................................................................
  • Difference between Aggregation and Composition...............................................................................
    • Aggregation.......................................................................................................................................
    • Composition......................................................................................................................................
    • Aggregation vs Composition..............................................................................................................
  • Encapsulation........................................................................................................................................
  • Differences between Overriding, Overloading......................................................................................
    • Overloading.......................................................................................................................................
    • Overriding..........................................................................................................................................
    • Overriding vs Overloading.................................................................................................................
  • Differences between Interfaces and Abstract classes............................................................................
    • Interfaces...........................................................................................................................................
    • Abstract classes.................................................................................................................................
    • Interfaces vs abstract classes.............................................................................................................
  • Bibliography...............................................................................................................................................

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.

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 Example

Polymorphism

Polymorphism is the capacity of an object interpretation of many structures. The most well- known utilization of polymorphism in OOP happens when a parent class reference is utilized to allude to a youngster class question. Any Java question that can pass more than one IS-A test is thought to be polymorphic. In Java, all Java objects are polymorphic since any protest will breeze through the IS-A test for their own particular sort and for the class Object.

Abstraction

Abstraction is a procedure of hiding the usage subtle elements and demonstrating just usefulness to the user. Another way, it demonstrates just imperative things to the client and conceals the inward subtle elements, for instance, sending SMS, you simply sort the content and send the message. You don't have the foggiest idea about the inward handling about the message conveyance.

A class which contains the abstract keyword in its declaration is known as abstract class.  Abstract classes may or may not contain abstract methods, i.e., methods without body (public void get () ;)  But, if a class has at least one abstract method, then the class must be declared abstract.  If a class is declared abstract, it cannot be instantiated. Example

Association

is a relationship between two separate classes which can be of any sort say coordinated, one to May and so on. It joins two totally isolate elements. Conglomeration is an uncommon type of affiliation which is a unidirectional one route connection between classes (or elements), for e.g. Wallet and Money classes Example

Composition

The composition is a limited type of Aggregation in which two elements are exceptionally subject to each other.  It represents part of a relationship.  In creation, both the elements are subject to each other.  At the point when there is a piece between two substances, the made question can't exist without the other element.

Aggregation vs Composition

 Dependency: Aggregation suggests a relationship where the child can exist independently of the parent. For instance, Bank and Employee, erase the Bank and the

Employee still exist. while Composition infers a relationship where the child can't exist free of the parent. Case: Human and heart, heart don't exist separate to a Human  Kind of Relationship: Aggregation connection is "has-a" and synthesis is "a piece of" connection.  Kind of affiliation: Composition is a solid Association while Aggregation is a feeble Association.

Encapsulation

Encapsulation is one of the four essential OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a system of wrapping the information (factors) and code following up on the information (methods) together as a solitary unit. In an embodiment, the factors of a class will be avoided different classes and can be gotten to just through the techniques for their present class. In this manner, it is otherwise called information covering up. To accomplish Encapsulation in Java  Proclaim the factors of a class as private.  Give open setter and getter techniques to change and view the factors esteems. Example

records are distinctive. In the last instructional exercise, we talked about constructor overloading that enables a class to have more than one constructors having diverse contention records.

Overriding

In the event that subclass (child class) has the same method from declared in the parent class, it is known as method overriding in java. If subclass gives the particular execution of the strategy that has been given by one of its method class, it is known as method overriding.

Rules for Java Method Overriding

 method must have same name as in the parent class  method must have same parameter as in the parent class.

 must be IS-A relationship (inheritance).

Overriding vs Overloading

 The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time.  Polymorphism applies to overriding, not to overloading.  Overriding is a run-time concept while overloading is a compile-time concept.

  • The interface keyword is used to declare interface.

Abstract classes

Abstract classes are like interfaces. You can't instantiate them, and they may contain a blend of methods declared with or without a usage. In any case, with abstract classes, you can declare fields that are not static and final, and characterize public, protected, and private concrete methods

Key points

 An abstract class has no use until unless it is extended by some other class.  If you declare an abstract method (discussed below) in a class then you must declare the class abstract as well. you can’t have abstract method in a non-abstract class. It’s vice versa is not always true: If a class is not having any abstract method then also it can be marked as abstract.  Abstract class can have non-abstract method (concrete) as well.

Interfaces vs abstract classes

abstract Classes Interfaces

1 abstract class can extend only one class or one abstract class at a time interface can extend any number of interfaces at a time 2 abstract class can extend from a class or from an abstract class interface can extend only from an interface 3 abstract class can have both abstract and concrete methods interface can have only abstract methods 4 A class can extend only one abstract class A class can implement any number of interfaces

Bibliography

o beginnersbook, 2017. Abstract Classes and Methods in Java. [Online] Available at: http://beginnersbook.com/2013/05/java-abstract-class-method/ [Accessed 28th June 2017].