In this session you will learn:
Encapsulation
Inheritance
For more information, visit: https://www.mindsmapped.com/courses/software-development/java-developer-training-for-beginners/
3. Page 3Classification: Restricted
• Implement encapsulation
• by marking the field as private.
• by marking the member function as private.
Encapsulation
4. Page 4Classification: Restricted
• avoids reinventing the wheel
• promotes code reusability
• derives a new class from the existing class
• use the existing features
• enhance the existing features
• add new features.
• Existing class is called as “base class”
• new class derived from existing class is called as “Child class”
Inheritance
5. Page 5Classification: Restricted
• child class will have all the features from the parent class
• plus it will have its own additional features
• additional features:
• new member variable ;
• new member functions
• What can you access in child class from parent class
• non private member variables
• non private member variables can be access via setter and getter
methods of parent class.
• non private member functions
• What can you do in Child class
• access non private data from parent class directly.
• inherited methods can be accessed directly.
• declare new methods in child class.
• override parent class methods in child class.
Inheritance
6. Page 6Classification: Restricted
Sms
+ String to
+ String text
+ void sendSms()
PictureSms
+ String to
+ String text
+ String pic
+ void sendSms()
+void sendPicSms()
11. Page 11Classification: Restricted
Constructors….revisited
• Constructor are not inherited
• When a child class object is created,
• derive class constructor invokes its immediate super class constructor,
all the way up the hierarchy.
• How can a child class constructor call its parent class constructor.
• using super keyword
12. How constructors are invoked Rectangle
+ int length
+ int breadth
+ void area()
Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
Cuboid c = new Cuboid();
Rectangle
+ int length
+ int breadth
+ void area()
length = 0
breadth= 0 Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
height = 0
13. Cuboid c= new Cuboid() class Rectangle
{
public int length;breadth;
public Rectangle()
{
length = 0;
breadth =0;
}
}
class Cuboid
{
//private int length;breadth;
public int height;
public Cuboid()
{
super();
height = 0;
}
}
length=0
breadth=0
height =0
call to super is
inserted by the
compiler implicitly f
default constructor
14. Cuboid c= new Cuboid(10, 20,30) class Rectangle
{
public int length;breadth;
public Rectangle(int l, int
b)
{
length = l;
breadth =b;
}
}
class Cuboid
{
//private int length;breadth;
public int height;
public Cuboid(int l, int b,int
h)
{
super(l, b );
height = h;
}
}
length=10
breadth=20
height =30
call to super is explicit
15. Page 15Classification: Restricted
Super
• super keyword is used to call immediate parent class constructor
• should be the first statement of the constructor
• If the programmer does not provide call to super(), then the compiler
inserts the call to super.
• programmer must call super() explicitly for constructor with arguments.