SlideShare a Scribd company logo
1 of 12
Lecture 6
Encapsulation




        Object Oriented Programming
         Eastern University, Dhaka
                 Md. Raihan Kibria
What encapsulation means
   Broadly, it means keeping variables and
    methods together inside a class. In the
    following demo we will create a JFrame.
    We will also add two JPanels. We want
    each of the panels two have certains
    things:
      One text field
      One button
public class EncapsulationDemo {
  public static void main(String[] args) {
   JFrame jframe = new JFrame();
   jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   jframe.setBounds(0, 0, 300, 200);
   jframe.getContentPane().setLayout(new FlowLayout());

     MyPanel m = new MyPanel();
     jframe.getContentPane().add(m);

     m = new MyPanel();
     jframe.getContentPane().add(m);

     jframe.setVisible(true);
    }
}

class MyPanel extends JPanel{
  public MyPanel() {
    super();
    this.setBackground(Color.GRAY);
    this.addComponents();
  }

    private void addComponents(){
      JTextField text = new JTextField("Hello");
      this.add(text);
      JButton btn = new JButton("submit");
      this.add(btn);
    }
Here is the output




Notice whenever we make a new MyPanel we get a
JTextBox and a JButton for free

This happens because the method addComponents() gets
called. In other words, we have encapsulated the behaviour
of the object in the class definition
Encapsulation conclusion
   It is the essence of object oriented
    programming
   Keeping functionalities/behavior embedded
    into an object gives us the benefit of having
    free functionality when we create an object
   We cannot do this in struct of C
Inheritance
   Inheritance can be achived when we
    “extend” a class using 'extends” keyword.
   The super class or base class is the original
    class. The child class is the new class.

   Example: Jframe looks like this:
Code to make a JFrame
 import javax.swing.JFrame;

 public class InheritanceDemoFrame {

     public static void main(String[] args) {
       JFrame jframe = new JFrame();
       jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jframe.setBounds(0, 0, 300, 200);
       jframe.setVisible(true);
     }
 }



Note that the JFrame shown can be maximized, closed,
minimized, etc.
Let us extend the JFrame
public class InheritanceDemoFrame extends JFrame{

    InheritanceDemoFrame(){
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(0, 0, 300, 200);
      setLayout(new FlowLayout());

        JButton jbutton = new JButton("Test button");
        getContentPane().add(jbutton);

        JTextField jtext = new JTextField();
        getContentPane().add(jtext);
        jtext.setText("Hello");
    }

    public static void main(String[] args) {
      InheritanceDemoFrame f = new InheritanceDemoFrame();
      f.setVisible(true);
    }
}
The output




Lesson: By “extend”ing the JFrame we still have
maximize, close, minimize buttons. We have added
more things without loosing any previous
functionalities
Inheritance features
   We can extend a class if it is not marked
    final
   When we extends we inherit its public,
    protected methods and variables
   We can further extends and inherited class
   Inheritance is a key feature of object
    oriented programming
More features of inheritance
   The base class is called super class
   The child class is called sub class
   Members (variables and methods) of the
    super class are accessible from the sub
    class using super keyword. The other way
    access is not possible or make any sense
More features of inheritance
   The members of the sub class can be referred to using “this”
           class A{
             public int i;

               public void increment(){
                 i = i + 1;
               }
           }

           class B extends A{

               public int i;

               public void increment(){
                 super.i = super.i + 1;
               }

               public void incrementMe(){
                 this.i = this.i + 1;
               }

               public void incrementDad(){
                 super.increment();
               }

More Related Content

What's hot

Lab 4 jawapan (sugentiran mane)
Lab 4 jawapan (sugentiran mane)Lab 4 jawapan (sugentiran mane)
Lab 4 jawapan (sugentiran mane)
Yugeswary
 

What's hot (12)

Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Lab 4 jawapan (sugentiran mane)
Lab 4 jawapan (sugentiran mane)Lab 4 jawapan (sugentiran mane)
Lab 4 jawapan (sugentiran mane)
 
Core java oop
Core java oopCore java oop
Core java oop
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 

Viewers also liked (9)

Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Calendario portada
Calendario portadaCalendario portada
Calendario portada
 
Oop lecture1
Oop lecture1Oop lecture1
Oop lecture1
 
Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 
Oop lecture8
Oop lecture8Oop lecture8
Oop lecture8
 
Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]
 
Cwgd
CwgdCwgd
Cwgd
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 

Similar to Oop lecture6

Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
I am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdfI am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdf
seamusschwaabl99557
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
alliedscorporation
 

Similar to Oop lecture6 (20)

Oop lecture9
Oop lecture9Oop lecture9
Oop lecture9
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Lecture 7.pdf
Lecture 7.pdfLecture 7.pdf
Lecture 7.pdf
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Spring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergrationSpring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergration
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
I am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdfI am sorry but my major does not cover programming in depth (ICT) an.pdf
I am sorry but my major does not cover programming in depth (ICT) an.pdf
 
Computer programming 2 -lesson 4
Computer programming 2  -lesson 4Computer programming 2  -lesson 4
Computer programming 2 -lesson 4
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
 

More from Shahriar Robbani (7)

Group111
Group111Group111
Group111
 
SQL
SQLSQL
SQL
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Oop lecture3
Oop lecture3Oop lecture3
Oop lecture3
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

Oop lecture6

  • 1. Lecture 6 Encapsulation Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. What encapsulation means  Broadly, it means keeping variables and methods together inside a class. In the following demo we will create a JFrame. We will also add two JPanels. We want each of the panels two have certains things:  One text field  One button
  • 3. public class EncapsulationDemo { public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.getContentPane().setLayout(new FlowLayout()); MyPanel m = new MyPanel(); jframe.getContentPane().add(m); m = new MyPanel(); jframe.getContentPane().add(m); jframe.setVisible(true); } } class MyPanel extends JPanel{ public MyPanel() { super(); this.setBackground(Color.GRAY); this.addComponents(); } private void addComponents(){ JTextField text = new JTextField("Hello"); this.add(text); JButton btn = new JButton("submit"); this.add(btn); }
  • 4. Here is the output Notice whenever we make a new MyPanel we get a JTextBox and a JButton for free This happens because the method addComponents() gets called. In other words, we have encapsulated the behaviour of the object in the class definition
  • 5. Encapsulation conclusion  It is the essence of object oriented programming  Keeping functionalities/behavior embedded into an object gives us the benefit of having free functionality when we create an object  We cannot do this in struct of C
  • 6. Inheritance  Inheritance can be achived when we “extend” a class using 'extends” keyword.  The super class or base class is the original class. The child class is the new class.  Example: Jframe looks like this:
  • 7. Code to make a JFrame import javax.swing.JFrame; public class InheritanceDemoFrame { public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.setVisible(true); } } Note that the JFrame shown can be maximized, closed, minimized, etc.
  • 8. Let us extend the JFrame public class InheritanceDemoFrame extends JFrame{ InheritanceDemoFrame(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0, 0, 300, 200); setLayout(new FlowLayout()); JButton jbutton = new JButton("Test button"); getContentPane().add(jbutton); JTextField jtext = new JTextField(); getContentPane().add(jtext); jtext.setText("Hello"); } public static void main(String[] args) { InheritanceDemoFrame f = new InheritanceDemoFrame(); f.setVisible(true); } }
  • 9. The output Lesson: By “extend”ing the JFrame we still have maximize, close, minimize buttons. We have added more things without loosing any previous functionalities
  • 10. Inheritance features  We can extend a class if it is not marked final  When we extends we inherit its public, protected methods and variables  We can further extends and inherited class  Inheritance is a key feature of object oriented programming
  • 11. More features of inheritance  The base class is called super class  The child class is called sub class  Members (variables and methods) of the super class are accessible from the sub class using super keyword. The other way access is not possible or make any sense
  • 12. More features of inheritance  The members of the sub class can be referred to using “this” class A{ public int i; public void increment(){ i = i + 1; } } class B extends A{ public int i; public void increment(){ super.i = super.i + 1; } public void incrementMe(){ this.i = this.i + 1; } public void incrementDad(){ super.increment(); }