110 likes | 136 Views
OOP Concepts has become an important part of software development. Writing Programs using Top down approach of traditional structured programming or Functional Programming method becomes difficult as the complexity of the program increases. This is where Object-oriented programming has its upper hand.
E N D
Learn 5 OOP Concepts in Java [With CodeExamples] OOP Concepts has become an important part of software development. Writing Programs using Top down approach of traditional structured programming or Functional Programming method becomes difficult as the complexity of the program increases. This is where Object-oriented programming has its upper hand. OOP Concepts allow you to break these complex programs into small bites, which you can solve one by one. OOP is an effective model that allows your program to grow without becoming impossible to maintain. Add to it ubiquity of languages like Java and OOP Principles become need of the hour. OOP is a whole new way to address problems. Here you create a class, instead of writing a program, which has variables and functions. Objects are self-contained examples of that class and you have a control where you can make them interact with each other in many ways. This kind of encapsulation makes knowledge of Object-oriented Programming and its main Principles a must if you are serious about working on big projects and developing software for mobiles. There are many important features of Object-Oriented programming, however, before proceeding further we suggest you read Learn 7 Basics of Java Programming to Start Coding Today which helps you to know all the basic concepts of Java. Discussion in this blog is divided into following structure: I. Object Oriented programming Concepts in Java.
II. III. IV. OOP Best practices. Advantages of Java OOP concepts. Differences between object oriented and object-based program. Let us dive right into main Principles of object-oriented programming and discuss them with relevant examples. What are OOP concepts? Object oriented programming is a method based on the concepts of “objects” which contains data and methods. Its main objective is to bring flexibility in programming while building small and large software applications. Most of the programming language like Java, C++, Python, C#, etc. follow OOP concepts. We will address following OOP Concepts In Java: I. II. III. IV. V. VI. VII. Object Class What are all the difference between Object and Class? Inheritance Polymorphism Encapsulation Abstraction What is an Object? An object is an entity which has both state and behavior. An object can be defined as an instance of a class, which contains the address and takes up some space in the memory.
Some of the key points of objects are: I. II. III. It is having a state. It is having behavior. It is having an identity. Consider an example to understand what is object? Consider writing pen as an example. Here pen is an object and its name is Reynolds, and color is white, known as its state. Pen is used to write is its behavior. A keyword “new” is used to create objects in Java. Object is created with the following syntax: className ReferenceVariable = new ClassName(); What is a Class? Class can be considered as blueprint using which many numbers of objects can be created. It is a group of objects which have common properties. Class contains variables and methods to describe the behavior of an object. Consider a house as an example to understand what is class? Here the house is the object. All the properties of the house such as floors, doors, window, etc. can be considered as the different classes.
Syntax to declare class is: class <class_name> { Field; Methods; } A class in Java contains: I. II. III. IV. V. Fields. Methods. Constructors. Blocks. Nested class and interface. What is the difference between Object and Class? Object and class are the main concepts of Objecting Oriented programming concepts. We will discuss some of the difference between these two concepts.
Once we have learned the Object and Class, then we will see further OOPs concepts of Java. Inheritance: It can be defined as the process where one class acquires the properties of another class using a keyword called ‘extends’. Here the class which inheritance the properties of other class is called ‘subclass’ and the class whose properties are inherited are called ‘superclass’. Inheritance is mainly used for code reusability. So, here you are making use of existing classes and further extending on that. Let us look into a programming example of inheritance concept. Let us look for a sample inheritance program. package com.inheritance; class Teamlead{ float salary=60000; } class Javaprogrammer extends Teamlead{ publicstaticvoid main(String args[]){ Javaprogrammer p=new Javaprogrammer(); System.out.println("Java programmer salary is:"+p.salary); } }
Output: Java programmer salary is:60000.0 Different types of inheritance: 1. Single inheritance: It is a type of inheritance in which one class inheritance the property of another class. Let us the see the syntax: Class A { ---- } Class B extends A { ---- } 2. Multilevel inheritance: In this type of inheritance one class is derived from a class which is also derived from another class i.e., a class is having more than one parent class but at different levels. Let us see the syntax: Class A{
--- } Class B extends A{ --- } Class C extends B{ --- } 3.Hierarchical inheritance: It is a type of inheritance in which a class has more than one child class or in other words, more than one child class have the same parent class. Let us see the syntax: Class A { ----
} Class B extends A { ---- } Class C extends A { ---- } 4.Hybrid inheritance: It is a combination of multiple inheritance and multilevel inheritance.
Polymorphism: It is object-oriented programming feature which allows the programmer to perform a single action in many ways. The common use of polymorphism is when a parent class reference is used to refer a child class object. Different types of polymorphism: I. Static polymorphism: This is a type of polymorphism which gets resolved during compilation. It is also called compile time polymorphism. Dynamic polymorphism: This is a type of polymorphism in which call to an overridden method is resolved at runtime. So, it is called runtime polymorphism. II. Encapsulation: Encapsulation means binding object state and behavior together. It is a mechanism of wrapping the data (variable) and code acting on the data (methods) together as a single unit. It is a called as data hiding as variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. An encapsulated class can be created by making all the data members of the class private. Abstraction: Abstraction is a process of hiding the implementation details from the user, here only the functionality will be provided to the user. Abstract class contains ‘abstract’ keyword. Here you show only relevant data and hide unnecessary details of an object from the user.
Let us discuss Abstraction with an example: Consider an example wherein you are using your bank account online, to login you will enter your user_id and password and then press the login button, after pressing login button you will not know how the input data is sent to the server and how it gets verified as all this information is abstracted away from you. What are the best practices of OOP concept? The overall vision of implementing object-oriented program is to develop a secure and more reliable software application. This requires following some proper guidelines while writing the programs which we will discuss below: I. Single responsibility: One of the important best practices that every developer needs to follow is the “Single Responsibility Principle”. Here in object oriented programming the class should always have one functionality so that it is called or extended whenever new usage arises, without causing coupling between different functionalities. Open closed design: This principle says that keep all the methods and classes need to be closed for modifications but open for extensions. It means to create software entities whose behavior can be changed without editing and recompiling the code itself. Liskov Substitution Principle: This principle states that subtypes may be substitutable for their base types. Interface Segregation Principle: This principle states that a client should not implement an interface if it doesn’t use that. Dependency Inversion Principle: This principle states that high-level modules, which provides complex logic, should be easily reusable and they need to be unaffected by the changes in low level modules, which provides utility features. Abstraction can be used which decouples high-level and low-level modules from each other. II. III. IV. V. Advantages of Java OOP concepts: I. II. OOP concepts reduce the complexity in coding and presents a good program structure. As per the requirements adding new features or responding to changing operating environments can be easily done by introducing a new few objects and precisely modify the existing code without affecting the present code. OOP introduces the reusability function as objects can be reused across applications which helps in faster development. OOP provides modular structure for programs wherein abstract datatypes can be defined which helps to hide the implementation details. III. IV. Differences between object oriented and object-based program: There are few differences exists between object-oriented programming and object-based programming language.
Conclusion: In this Java blog, we have discussed some of the OOPs concepts like Object, Class, Inheritance, Polymorphism, Encapsulation, and Abstraction. Along with this, we have also seen the advantages of OOP concept and many other topics. Hope this helped our readers to understand why we need OOP Concepts. Let us know with which concept you will start your learning roadmap. Here at Simpliv you can find vast library of free and paid online Java courses which can help you in this journey. Either way, let me know by leaving a comment right below and do share the post on social media if you have enjoyed reading it.