KEMBAR78
Java Encapsulation and Inheritance | PPTX
Java/J2EE Programming Training
Java Inheritance
Page 2Classification: Restricted
Agenda
• Encapsulation
• Inheritance
Page 3Classification: Restricted
• Implement encapsulation
• by marking the field as private.
• by marking the member function as private.
Encapsulation
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
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
Page 6Classification: Restricted
Sms
+ String to
+ String text
+ void sendSms()
PictureSms
+ String to
+ String text
+ String pic
+ void sendSms()
+void sendPicSms()
Page 7Classification: Restricted
Rectangle
+ int length
+ int breadth
+ void area()
Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
Page 8Classification: Restricted
Student
String username
String passwd
String course
Int score
login()
logoff
writeExam()
viewResult
Employee
String username
String passwd
String desig
Int salary
login()
logoff
evaluate()
Page 9Classification: Restricted
Student
String username
String password
String course
Int marks
Login()
Logoff()
takeExam()
Employee
String username
String password
String desig
Int salary
Login()
Logoff()
evaluate()
User
String username
String password
Login()
Logoff()
Page 10Classification: Restricted
Question
+ String question
+ String optionA
+ String optionB
+ char answer
+ void addQuestion()
+ void deleteQuestion
ImageQuestion
+ String question
+ String optionA
+ String optionB
+ char answer
+ Image img
+ void addQuestion()
+ void deleteQuestion
+ void addImage()
VideoQuestion
+ String question
+ String optionA
+ String optionB
+ char answer
+ Video video
+ void addQuestion()
+ void deleteQuestion
+ void addVideo()
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
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
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
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
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.
Page 16Classification: Restricted
Thank You

Java Encapsulation and Inheritance

  • 1.
  • 2.
    Page 2Classification: Restricted Agenda •Encapsulation • Inheritance
  • 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()
  • 7.
    Page 7Classification: Restricted Rectangle +int length + int breadth + void area() Cuboid + int length + int breadth + int height + void surfaceArea() +void volume()
  • 8.
    Page 8Classification: Restricted Student Stringusername String passwd String course Int score login() logoff writeExam() viewResult Employee String username String passwd String desig Int salary login() logoff evaluate()
  • 9.
    Page 9Classification: Restricted Student Stringusername String password String course Int marks Login() Logoff() takeExam() Employee String username String password String desig Int salary Login() Logoff() evaluate() User String username String password Login() Logoff()
  • 10.
    Page 10Classification: Restricted Question +String question + String optionA + String optionB + char answer + void addQuestion() + void deleteQuestion ImageQuestion + String question + String optionA + String optionB + char answer + Image img + void addQuestion() + void deleteQuestion + void addImage() VideoQuestion + String question + String optionA + String optionB + char answer + Video video + void addQuestion() + void deleteQuestion + void addVideo()
  • 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 areinvoked 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= newCuboid() 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= newCuboid(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.
  • 16.