SlideShare a Scribd company logo
Java Object Oriented Programming concepts 
Introduction 
This tutorial will help you to understand about Java OOP’S concepts with 
examples. Let’s discuss about what are the features of Object Oriented 
Programming. Writing object-oriented programs involves creating classes, 
creating objects from those classes, and creating applications, which are stand-alone 
executable programs that use those objects. 
A class is a template, blueprint,or contract that defines what an object’s data 
fields and methods will be. An object is 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 are invoked to create a new object. A 
constructor can perform any action, but constructors are designed to perform 
initializing actions, such as initializing the data fields of objects. 
Objects are made up of attributes and methods. Attributes are the characteristics 
that define an object; the values contained in attributes differentiate objects of the 
same class from one another. To understand this better let’s take example of 
Mobile as object. Mobile has characteristics like model, manufacturer, cost, 
operating system etc. So if we create “Samsung” mobile object and “IPhone” 
mobile object we can distinguish them from characteristics. The values of the 
attributes of an object are also referred to as the object’s state. 
There are three main features of OOPS. 
1) Encapsulation
2) Inheritance 
3) Polymorphism 
Let’s we discuss about the about features in details. 
Encapsulation 
Encapsulation means putting together all the variables (instance variables) and 
the methods into a single unit called Class. 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 that you can use without regard 
to the internal 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 be set once while creating object using 
constructor arguments. Properties can be accessed using getXXX() methods 
which are having public access modifiers. 
1. package oopsconcept; 
2. public class Mobile { 
3. private String manufacturer; 
4. private String operating_system; 
5. public String model; 
6. private int cost; 
7. //Constructor to set properties/characteristics of object 
8. Mobile(String man, String o,String m, int c){ 
9. this.manufacturer = man; 
10. this.operating_system=o; 
11. this.model=m; 
12. this.cost=c; 
13. } 
14. //Method to get access Model property of Object 
15. public String getModel(){ 
16. return this.model; 
17. } 
18. // We can add other method to get access to other properties 
19. } 
Inheritance 
An important feature of object-oriented programs is 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 you are 
making use of already written class and further extending on that. That why we 
discussed about the code reusability the concept. In general one line definition 
we can tell that 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 
Mobile class extended by other specific class like Android and Blackberry.
1. package oopsconcept; 
2. public class Android extends Mobile{ 
3. //Constructor to set properties/characteristics of object 
4. Android(String man, String o,String m, int c){ 
5. super(man, o, m, c); 
6. } 
7. //Method to get access Model property of Object 
8. public String getModel(){ 
9. return "This is Android Mobile- " + model; 
10. } 
11. } 
1. package oopsconcept; 
2. public class Blackberry extends Mobile{ 
3. //Constructor to set properties/characteristics of object 
4. Blackberry(String man, String o,String m, int c){ 
5. super(man, o, m, c); 
6. } 
7. public String getModel(){ 
8. return "This is Blackberry-"+ model; 
9. } 
10. } 
Polymorphism 
In Core Java Polymorphism is one of 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 be interpreted 
correctly in different situations based on the context. There are two types of 
Polymorphism available in Java. For example, in English the verb “run” means 
different things if you use it with “a footrace,” a “business,” or “a computer.” You 
understand the meaning of “run” based on the other words used with it. Object-oriented 
programs are written so that the methods having same name works 
differently in different context. Java provides two 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 method overloading. In below program 
we have 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 the method executes. 
1. package oopsconcept; 
2. class Overloadsample { 
3. public void print(String s){ 
4. System.out.println("First Method with only String- "+ s);
5. } 
6. public void print (int i){ 
7. System.out.println("Second Method with only int- "+ i); 
8. } 
9. public void print (String s, int i){ 
10. 
11. System.out.println("Third Method with both- "+ s + "—" + i); 
12. } 
13. } 
14. public class PolymDemo { 
15. public static void main(String[] args) { 
16. Overloadsample obj = new Overloadsample(); 
17. obj.print(10); 
18. obj.print("Amit"); 
19. obj.print("Hello", 100); 
20. } 
21. } 
Output : 
Dynamic Polymorphism (run time polymorphism/ 
Method Overriding) 
When you create a subclass by extending an existing class, the new subclass 
contains data and methods that were defined in the original superclass. In other 
words, any child class object has all the attributes of its parent. Sometimes, 
however, the superclass data fields and methods are not entirely appropriate for 
the subclass objects; in these cases, you want to override the parent class 
members. Let’s take example used in inheritance explanation. 
1. package oopsconcept; 
2. public class OverridingDemo { 
3. public static void main(String[] args) { 
4. //Creating Object of SuperClass and calling getModel Method 
5. Mobile m = new Mobile("Nokia", "Win8", "Lumia",10000); 
6. System.out.println(m.getModel()); 
7. //Creating Object of Sublcass and calling getModel Method 
8. Android a = new Android("Samsung", "Android", "Grand",30000); 
9. System.out.println(a.getModel()); 
10. //Creating Object of Sublcass and calling getModel Method
11. Blackberry b = new Blackberry("BlackB", "RIM", "Curve",20000); 
12. System.out.println(b.getModel()); 
13. } 
14. } 
Abstraction 
All programming languages provide abstractions. It can be argued that the 
complexity of the problems you’re able to solve is directly related to the kind and 
quality of abstraction. An essential element of object-oriented programming is 
abstraction. Humans manage complexity through abstraction. When you drive 
your car you do not have to be concerned with the exact internal working of your 
car(unless you are a mechanic). What you are concerned with is interacting with 
your car via its interfaces like steering wheel, brake pedal, accelerator pedal etc. 
Various manufacturers of car has different implementation of car working but its 
basic interface has not changed (i.e. you still use steering wheel, brake pedal, 
accelerator pedal etc to interact with your car). Hence the knowledge you have of 
your car is abstract. 
A powerful way to manage abstraction is through the use of hierarchical 
classifications. This allows you to layer the semantics of complex systems, 
breaking them into more manageable pieces. From the outside, the car is a 
single object. Once inside, you see that the car consists of several subsystems: 
steering, brakes, sound system, seat belts, heating, cellular phone, and so on. In 
turn, each of these subsystems is made up of more specialized units. For 
instance, the sound system consists of a radio, a CD player, and/or a tape 
player. The point is that you manage the complexity of the car (or any other 
complex system)through the use of hierarchical abstractions. 
An abstract class is something which is incomplete and you can not create 
instance of abstract class. If you want to use it you need to make it complete or 
concrete by extending it. A class is called concrete if it does not contain any 
abstract method and implements all abstract method inherited from abstract 
class or interface it has implemented or extended. By the way Java has concept 
of abstract classes, abstract method but a variable can not be 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 be methods like start() and Stop() but 
don’t know start and stop mechanism of every vehicle since they could have 
different start and stop mechanism e.g some can be started by kick or some can 
be by pressing buttons. 
Advantage of Abstraction is if there is new type of vehicle introduced we might 
just need to add one class which extends Vehicle Abstract class and implement 
specific methods. Interface of start and stop method would be same. 
1. package oopsconcept; 
2. 
3. public abstract class VehicleAbstract { 
4.
5. public abstract void start(); 
6. 
7. public void stop(){ 
8. System.out.println("Stopping Vehicle in abstract class"); 
9. } 
10. } 
11. 
12. class TwoWheeler extends VehicleAbstract{ 
13. 
14. @Override 
15. public void start() { 
16. System.out.println("Starting Two Wheeler"); 
17. } 
18. } 
19. class FourWheeler extends VehicleAbstract{ 
20. 
21. @Override 
22. public void start() { 
23. System.out.println("Starting Four Wheeler"); 
24. 
25. } 
26. 
27. } 
view plainprint? 
1. package oopsconcept; 
2. 
3. public class VehicleTesting { 
4. 
5. public static void main(String[] args) { 
6. VehicleAbstract my2Wheeler = new TwoWheeler(); 
7. VehicleAbstract my4Wheeler = new FourWheeler(); 
8. 
9. my2Wheeler.start(); 
10. my2Wheeler.stop(); 
11. 
12. my4Wheeler.start(); 
13. my4Wheeler.stop(); 
14. } 
15. } 
Output :
Summary 
 An object is 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 is 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. 
Author: Syeful Islam

More Related Content

PDF
Object Oriented Programming using JAVA Notes
DOC
Delphi qa
DOC
Advanced core java
DOCX
Object Oriented Programming in Android Studio
PPTX
Structural pattern 3
DOCX
Core java notes with examples
PPT
Java căn bản - Chapter2
Object Oriented Programming using JAVA Notes
Delphi qa
Advanced core java
Object Oriented Programming in Android Studio
Structural pattern 3
Core java notes with examples
Java căn bản - Chapter2

What's hot (20)

PPTX
Overloading and overriding in vb.net
PPT
JAVA CONCEPTS
PDF
Classes, objects, methods, constructors, this keyword in java
PPTX
Polymorphism in java
PDF
PDF ON JAVA - JFC CONCEPT
PPTX
Lecture 17
PPTX
Lecture 12
PPTX
Polymorphism presentation in java
PDF
java-06inheritance
PPT
Classes & objects new
PPT
Oops in Java
PPT
Lect 1-class and object
PPTX
Inheritance Mixins & Traits
PPTX
oops concept in java | object oriented programming in java
PPT
Reflection
PPTX
Java Unit 2(Part 1)
PPTX
Java Unit 2(part 3)
PPTX
Java Beans Unit 4(Part 1)
PPTX
Java Unit 2 (Part 2)
PDF
Polymorphism in Java
Overloading and overriding in vb.net
JAVA CONCEPTS
Classes, objects, methods, constructors, this keyword in java
Polymorphism in java
PDF ON JAVA - JFC CONCEPT
Lecture 17
Lecture 12
Polymorphism presentation in java
java-06inheritance
Classes & objects new
Oops in Java
Lect 1-class and object
Inheritance Mixins & Traits
oops concept in java | object oriented programming in java
Reflection
Java Unit 2(Part 1)
Java Unit 2(part 3)
Java Beans Unit 4(Part 1)
Java Unit 2 (Part 2)
Polymorphism in Java
Ad

Viewers also liked (14)

PDF
Http _economia.elpais
PPTX
Trabajo de ingles venecia oscar
PPTX
El rapte d'Amfítrite
PDF
خطه دفع شركه ويب فيندر هوست والارباح الخياليه
PPTX
Nanomedicine
PPTX
Sevilla lucia garcia
PPT
Question 7
PDF
A New Approach: Automatically Identify Naming Word from Bengali Sentence for ...
PPTX
E crm assignment-conversion_talent
PDF
From simple curiosity to "less bad" research in social science
PPTX
Omkar one race course
PPTX
8 Reasons Hearing Loss is More Dangerous Than You Think
PPTX
Question 6
PPT
Vera Bradley Web Analysis
Http _economia.elpais
Trabajo de ingles venecia oscar
El rapte d'Amfítrite
خطه دفع شركه ويب فيندر هوست والارباح الخياليه
Nanomedicine
Sevilla lucia garcia
Question 7
A New Approach: Automatically Identify Naming Word from Bengali Sentence for ...
E crm assignment-conversion_talent
From simple curiosity to "less bad" research in social science
Omkar one race course
8 Reasons Hearing Loss is More Dangerous Than You Think
Question 6
Vera Bradley Web Analysis
Ad

Similar to Java oop concepts (20)

PDF
JAVA-PPT'S.pdf
PPTX
JAVA-PPT'S-complete-chrome.pptx
PPTX
JAVA-PPT'S.pptx
PPTX
ITTutor Advanced Java (1).pptx
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
PDF
Oops concepts
PPTX
Introduction to OOP concepts
PPTX
OOPS (Object Oriented Programming System) CONCEPTS
PDF
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
PDF
Java Programming Paradigms Chapter 1
PPTX
java part 1 computer science.pptx
PPTX
Java OOPS Concept
PPTX
Java and its concepts
PPTX
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
PPTX
Introduction to OOPs second year cse.pptx
PPSX
Oop features java presentationshow
PPTX
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
PPT
Java OOP s concepts and buzzwords
PPTX
Android Training (Java Review)
PPT
Md02 - Getting Started part-2
JAVA-PPT'S.pdf
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S.pptx
ITTutor Advanced Java (1).pptx
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
Oops concepts
Introduction to OOP concepts
OOPS (Object Oriented Programming System) CONCEPTS
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Programming Paradigms Chapter 1
java part 1 computer science.pptx
Java OOPS Concept
Java and its concepts
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
Introduction to OOPs second year cse.pptx
Oop features java presentationshow
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Java OOP s concepts and buzzwords
Android Training (Java Review)
Md02 - Getting Started part-2

More from Syeful Islam (14)

PDF
[Brochure ] mobi manager mdm_software
PDF
Business benefit of Mobile device management
PDF
Mobi manager mdm
PDF
A New Approach: Automatically Identify Proper Noun from Bengali Sentence for ...
PDF
Hidden Object Detection for Computer Vision Based Test Automation System
PDF
A New Approach: Automatically Identify Naming Word from Bengali Sentence for ...
PDF
Disordered Brain Modeling Using Artificial Network SOFM
PDF
Disordered Brain Modeling Using Artificial Network SOFM
PPTX
jQuery besic
PDF
An Algorithm for Electronic Money Transaction Security (Three Layer Security)...
PDF
Design Analysis Rules to Identify Proper Noun from Bengali Sentence for Univ...
PDF
Performance Evaluation of Finite Queue Switching Under Two-Dimensional M/G/1...
PPT
Emergency notification at your phone
PPT
Development of analysis rules to identify proper noun from bengali sentence f...
[Brochure ] mobi manager mdm_software
Business benefit of Mobile device management
Mobi manager mdm
A New Approach: Automatically Identify Proper Noun from Bengali Sentence for ...
Hidden Object Detection for Computer Vision Based Test Automation System
A New Approach: Automatically Identify Naming Word from Bengali Sentence for ...
Disordered Brain Modeling Using Artificial Network SOFM
Disordered Brain Modeling Using Artificial Network SOFM
jQuery besic
An Algorithm for Electronic Money Transaction Security (Three Layer Security)...
Design Analysis Rules to Identify Proper Noun from Bengali Sentence for Univ...
Performance Evaluation of Finite Queue Switching Under Two-Dimensional M/G/1...
Emergency notification at your phone
Development of analysis rules to identify proper noun from bengali sentence f...

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Chapter 3 Spatial Domain Image Processing.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?

Java oop concepts

  • 1. Java Object Oriented Programming concepts Introduction This tutorial will help you to understand about Java OOP’S concepts with examples. Let’s discuss about what are the features of Object Oriented Programming. Writing object-oriented programs involves creating classes, creating objects from those classes, and creating applications, which are stand-alone executable programs that use those objects. A class is a template, blueprint,or contract that defines what an object’s data fields and methods will be. An object is 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 are invoked to create a new object. A constructor can perform any action, but constructors are designed to perform initializing actions, such as initializing the data fields of objects. Objects are made up of attributes and methods. Attributes are the characteristics that define an object; the values contained in attributes differentiate objects of the same class from one another. To understand this better let’s take example of Mobile as object. Mobile has characteristics like model, manufacturer, cost, operating system etc. So if we create “Samsung” mobile object and “IPhone” mobile object we can distinguish them from characteristics. The values of the attributes of an object are also referred to as the object’s state. There are three main features of OOPS. 1) Encapsulation
  • 2. 2) Inheritance 3) Polymorphism Let’s we discuss about the about features in details. Encapsulation Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. 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 that you can use without regard to the internal 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 be set once while creating object using constructor arguments. Properties can be accessed using getXXX() methods which are having public access modifiers. 1. package oopsconcept; 2. public class Mobile { 3. private String manufacturer; 4. private String operating_system; 5. public String model; 6. private int cost; 7. //Constructor to set properties/characteristics of object 8. Mobile(String man, String o,String m, int c){ 9. this.manufacturer = man; 10. this.operating_system=o; 11. this.model=m; 12. this.cost=c; 13. } 14. //Method to get access Model property of Object 15. public String getModel(){ 16. return this.model; 17. } 18. // We can add other method to get access to other properties 19. } Inheritance An important feature of object-oriented programs is 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 you are making use of already written class and further extending on that. That why we discussed about the code reusability the concept. In general one line definition we can tell that 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 Mobile class extended by other specific class like Android and Blackberry.
  • 3. 1. package oopsconcept; 2. public class Android extends Mobile{ 3. //Constructor to set properties/characteristics of object 4. Android(String man, String o,String m, int c){ 5. super(man, o, m, c); 6. } 7. //Method to get access Model property of Object 8. public String getModel(){ 9. return "This is Android Mobile- " + model; 10. } 11. } 1. package oopsconcept; 2. public class Blackberry extends Mobile{ 3. //Constructor to set properties/characteristics of object 4. Blackberry(String man, String o,String m, int c){ 5. super(man, o, m, c); 6. } 7. public String getModel(){ 8. return "This is Blackberry-"+ model; 9. } 10. } Polymorphism In Core Java Polymorphism is one of 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 be interpreted correctly in different situations based on the context. There are two types of Polymorphism available in Java. For example, in English the verb “run” means different things if you use it with “a footrace,” a “business,” or “a computer.” You understand the meaning of “run” based on the other words used with it. Object-oriented programs are written so that the methods having same name works differently in different context. Java provides two 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 method overloading. In below program we have 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 the method executes. 1. package oopsconcept; 2. class Overloadsample { 3. public void print(String s){ 4. System.out.println("First Method with only String- "+ s);
  • 4. 5. } 6. public void print (int i){ 7. System.out.println("Second Method with only int- "+ i); 8. } 9. public void print (String s, int i){ 10. 11. System.out.println("Third Method with both- "+ s + "—" + i); 12. } 13. } 14. public class PolymDemo { 15. public static void main(String[] args) { 16. Overloadsample obj = new Overloadsample(); 17. obj.print(10); 18. obj.print("Amit"); 19. obj.print("Hello", 100); 20. } 21. } Output : Dynamic Polymorphism (run time polymorphism/ Method Overriding) When you create a subclass by extending an existing class, the new subclass contains data and methods that were defined in the original superclass. In other words, any child class object has all the attributes of its parent. Sometimes, however, the superclass data fields and methods are not entirely appropriate for the subclass objects; in these cases, you want to override the parent class members. Let’s take example used in inheritance explanation. 1. package oopsconcept; 2. public class OverridingDemo { 3. public static void main(String[] args) { 4. //Creating Object of SuperClass and calling getModel Method 5. Mobile m = new Mobile("Nokia", "Win8", "Lumia",10000); 6. System.out.println(m.getModel()); 7. //Creating Object of Sublcass and calling getModel Method 8. Android a = new Android("Samsung", "Android", "Grand",30000); 9. System.out.println(a.getModel()); 10. //Creating Object of Sublcass and calling getModel Method
  • 5. 11. Blackberry b = new Blackberry("BlackB", "RIM", "Curve",20000); 12. System.out.println(b.getModel()); 13. } 14. } Abstraction All programming languages provide abstractions. It can be argued that the complexity of the problems you’re able to solve is directly related to the kind and quality of abstraction. An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. When you drive your car you do not have to be concerned with the exact internal working of your car(unless you are a mechanic). What you are concerned with is interacting with your car via its interfaces like steering wheel, brake pedal, accelerator pedal etc. Various manufacturers of car has different implementation of car working but its basic interface has not changed (i.e. you still use steering wheel, brake pedal, accelerator pedal etc to interact with your car). Hence the knowledge you have of your car is abstract. A powerful way to manage abstraction is through the use of hierarchical classifications. This allows you to layer the semantics of complex systems, breaking them into more manageable pieces. From the outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, sound system, seat belts, heating, cellular phone, and so on. In turn, each of these subsystems is made up of more specialized units. For instance, the sound system consists of a radio, a CD player, and/or a tape player. The point is that you manage the complexity of the car (or any other complex system)through the use of hierarchical abstractions. An abstract class is something which is incomplete and you can not create instance of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be 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 be methods like start() and Stop() but don’t know start and stop mechanism of every vehicle since they could have different start and stop mechanism e.g some can be started by kick or some can be by pressing buttons. Advantage of Abstraction is if there is new type of vehicle introduced we might just need to add one class which extends Vehicle Abstract class and implement specific methods. Interface of start and stop method would be same. 1. package oopsconcept; 2. 3. public abstract class VehicleAbstract { 4.
  • 6. 5. public abstract void start(); 6. 7. public void stop(){ 8. System.out.println("Stopping Vehicle in abstract class"); 9. } 10. } 11. 12. class TwoWheeler extends VehicleAbstract{ 13. 14. @Override 15. public void start() { 16. System.out.println("Starting Two Wheeler"); 17. } 18. } 19. class FourWheeler extends VehicleAbstract{ 20. 21. @Override 22. public void start() { 23. System.out.println("Starting Four Wheeler"); 24. 25. } 26. 27. } view plainprint? 1. package oopsconcept; 2. 3. public class VehicleTesting { 4. 5. public static void main(String[] args) { 6. VehicleAbstract my2Wheeler = new TwoWheeler(); 7. VehicleAbstract my4Wheeler = new FourWheeler(); 8. 9. my2Wheeler.start(); 10. my2Wheeler.stop(); 11. 12. my4Wheeler.start(); 13. my4Wheeler.stop(); 14. } 15. } Output :
  • 7. Summary  An object is 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 is 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. Author: Syeful Islam