12
Encapsulation Lecture 6 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Oop lecture6

Embed Size (px)

Citation preview

Page 1: Oop lecture6

EncapsulationLecture 6

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture6

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

Page 3: Oop lecture6

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

Page 4: Oop lecture6

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

Page 5: Oop lecture6

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

Page 6: Oop lecture6

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:

Page 7: Oop lecture6

Code to make a JFrameimport 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.

Page 8: Oop lecture6

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

Page 9: Oop lecture6

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

Page 10: Oop lecture6

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

Page 11: Oop lecture6

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

Page 12: Oop lecture6

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 features of inheritance