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

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 

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(); }