Oop lecture6

296 views
265 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
296
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture6

  1. 1. Lecture 6Encapsulation Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 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. 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. 4. Here is the outputNotice whenever we make a new MyPanel we get aJTextBox and a JButton for freeThis happens because the method addComponents() getscalled. In other words, we have encapsulated the behaviourof the object in the class definition
  5. 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. 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. 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. 8. Let us extend the JFramepublic 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. 9. The outputLesson: By “extend”ing the JFrame we still havemaximize, close, minimize buttons. We have addedmore things without loosing any previousfunctionalities
  10. 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. 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. 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(); }

×