Java Object Oriented Programming concepts

Source: Internet
Author: User


This tutorial is the help of understand about Java OOP's concepts with examples. Let's discuss about what is the features of Object oriented programming. Writing object-oriented programs involves creating classes, creating objects from those classes, and creating applications , which is stand-alone executable programs that use those objects.

A class is a template, Blueprint,or contract this defines what a object's data fields and methods would be. An object was an instance of a class. You can create many instances of a class. A Java class uses variables to define data fields and methods to define actions. Additionally,a class provides methods of a special type, known as constructors, which is invoked to create a new object. A constructor can perform any action, but constructors is designed to perform initializing actions, such as initializing The data fields of objects.

Objects is made up of attributes and methods. Attributes is the characteristics that define an object; The values contained in attributes differentiate objects of the same class from one another. To understand the better let's take example of Mobile as object. Mobile has characteristics like model, manufacturer, cost, operating system etc. So if we create the "Samsung" mobile object and "IPhone" mobile object we can distinguish them from characteristics. The values of the attributes of an object is also referred to as the object's state.

There is three main features of OOPS.

1) encapsulation

2) inheritance

3) polymorphism

Let's we discuss about the about features in details.


encapsulation means putting together all the variables (instance variables) and the methods to a single unit called Cl The. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes. Programmers sometimes refer to encapsulation as using a "black box," or a device so can use without regard Ternal mechanisms. A Programmer can access and use the methods and data contained in the black box but cannot change them. Below example shows Mobile class with properties, which can is set once while creating object using constructor arguments. Properties can be accessed using GetXXX () methods which is has public access modifiers.

Package Oopsconcept;public class Mobile {private string Manufacturer;private string Operating_system;public string model ;p rivate int Cost;//constructor to set Properties/characteristics of Objectmobile (String man, String o,string m, int c) {th Is.manufacturer = Man;this.operating_system=o;this.model=m;this.cost=c;} Method to get access Model property of Objectpublic String Getmodel () {return this.model;} We can add other method to get access to other properties}


an important feature of object-oriented programs are inheritance-the ability to create classes that share the attributes and methods of existing classes, but with more specific features. Inheritance is mainly used for code reusability. So is making use of already written class and further extending on that. That's why we discussed about the code reusability the concept. In the general one line definition we can tell this deriving a new class from existing class, it's called as inheritance. You can look into the following example for inheritance concept. Here we have the Mobile class extended by other specific class like Android and Blackberry.

Package Oopsconcept;public class Android extends mobile{//constructor to set properties/characteristics of objectandroid (String man, String o,string m, int c) {Super (man, O, M, c);} Method to get access Model property of Objectpublic String Getmodel () {return ' This is Android mobile-' + Model;}

Package Oopsconcept;public class Blackberry extends mobile{//constructor to set properties/characteristics of Objectblackberry (String Man, String o,string m, int c) {super (man, O, M, c);} Public String Getmodel () {return ' This is blackberry-' + model;}}

In Core Java polymorphism is one of the easy concept to understand. Polymorphism definition is that Poly means many and MorphOS means forms. It describes the feature of languages that allows the same word or symbol to being interpreted correctly in different Situati ONS based on the context. There is types of polymorphism available in Java. For example, in 中文版 the verb "run" means different things if you use it with "a footrace," a "business," or "a compute R. " You understand the meaning of "run" based on the other words used with it. object-oriented programs is written so, the methods has same name works differently in different context. Java provides ways to implement polymorphism.

Static polymorphism (compile time Polymorphism/method overloading):

The ability to execute different method implementations by altering the argument used with the method name is known as M Ethod overloading. In below program we had three print methods each with different arguments. When you properly overload a method, you can call it providing different argument lists, and the appropriate version of th E method executes.

Package Oopsconcept;class overloadsample {public void print (String s) {System.out.println ("first Method with only string- "+ s);} public void print (int i) {System.out.println ("Second Method with only int-" + i);} public void print (String s, int. i) {System.out.println ("third Method with both-" + S + "--" + i);}} public class Polymdemo {public static void main (string[] args) {overloadsample obj = new Overloadsample (); Obj.print (ten); OB J.print ("Amit"); Obj.print ("Hello", 100);}}

Dynamic polymorphism (run time Polymorphism/method overriding)

When you create a subclass by extending a existing class, the new subclass contains data and methods that were defined In the original superclass. In other words, an any child class object is the attributes of its parent. Sometimes, however, the superclass data fields and methods is not entirely appropriate for the subclass objects; In these cases, you want to override the parent class. Let's take the example used in inheritance explanation.

Package Oopsconcept;public class Overridingdemo {public static void main (string[] args) {//creating Object of superclass A nd calling Getmodel methodmobile m = new Mobile ("Nokia", "Win8", "Lumia", 10000); System.out.println (M.getmodel ());//creating Object of Sublcass and calling Getmodel methodandroid a = new Android ("Samsun G "," Android "," Grand ", 30000); System.out.println (A.getmodel ());//creating Object of Sublcass and calling Getmodel methodblackberry B = new Blackberry (" Blackb "," RIM "," Curve ", 20000); System.out.println (B.getmodel ());}}

All programming languages provide abstractions. It can be argued so the complexity of the problems you ' re able to solve are directly related to the kind and quality of a Bstraction. An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. When your drive your car does not has the concerned with the exact internal working of your car (unless is a mecha NIC). What's concerned with was interacting with your car via its interfaces like steering wheel, brake pedal, accelerator Pedal etc. Various manufacturers  of car have different implementation of car working but its basic interface have not changed (I. E. You still use steering wheel, brake pedal, accelerator pedal etc to interact with your car). Hence The knowledge you has the your car is abstract.

A Powerful-manage abstraction is through the use of hierarchical classifications. This allows-to-layer the semantics of complex systems, breaking them into more manageable pieces. From the outside, the car was a single object. Once inside, you see this car consists of several subsystems:steering, brakes, sound system, seat belts, heating, cel Lular phone, and so on. In turn, each of the these subsystems are made up to more specialized units. For instance, the sound system consists of a radio, a CD player, and/or a tape player. The Manage the complexity of the car (or any other complex system) through the use of hierarchical Abstra Ctions.

An abstract class is something which are incomplete and you can not create instance of abstract class. If you want-to-use it all need to make it complete or concrete by extending it. A class is called concrete if it does isn't contain any abstract method and implements all abstract method inherited from AB Stract class or interface it has implemented or extended. By the the-the-method Java has concept of the abstract classes, the abstract is a variable can not is abstract in Java.

Lets take an example of Java Abstract Class called Vehicle. When I am creating a class called Vehicle, I know there should is methods like start () and Stop () but don ' t know start and Stop mechanism of every vehicle since they could has different start and stop mechanism e.g some can be started by kick Or some can is by pressing buttons.

Advantage of abstraction is if there are new type of vehicle introduced we might just need to add one class which extends V  Ehicle Abstract class and implement specific methods. Interface of start and stop method would be same.

Package Oopsconcept;public Abstract class Vehicleabstract {public abstract void start ();p ublic void Stop () { System.out.println ("Stopping Vehicle in abstract class");}} Class Twowheeler extends vehicleabstract{@Overridepublic void Start () {System.out.println ("starting-Wheeler");}} Class Fourwheeler extends vehicleabstract{@Overridepublic void Start () {System.out.println ("starting four Wheeler");}}

Package Oopsconcept;public class Vehicletesting {public static void main (string[] args) {Vehicleabstract My2wheeler = new Twowheeler (); Vehicleabstract My4wheeler = new Fourwheeler (); My2wheeler.start (); My2wheeler.stop (); My4wheeler.start (); My4wheeler.stop ();}}

    • An object was an instance of a class.
    • Encapsulation provides the security that keeps data and methods safe from inadvertent changes.
    • Inheritance is parent-child relationship of class which are mainly used for code reusability.
    • Polymorphism definition is that Poly means many and MorphOS means forms.
    • Using abstraction one can simulate real world objects.
    • Abstraction provides advantage of code reuse
    • Abstraction enables program open for extension
-See more At:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.