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