Al Mizhar Dubai Escorts +971561403006 Escorts Service In Al Mizhar
Inheritence
1.
2. INHERITANCE
• The derivation if one class from another class is
called as INHERITANCE.
• You can inherit a class using the keyword extends.
3. IMPORTANT POINTS TO REMEMBER
• One derived class can extend only one base class.
• If we do not want to give some features of base class
through derived class then such features should be
made private.
• Constructor of one class never inherited to another class
• For each and every class in Java their exists an implicit
predefined super class known as java.lang.object.
5. SINGLE INHERITANCE
• One subclass is derived from only one super-class.
• Example:-
class Furniture {
int height=10, width=20;
public void disp_furniture()
{
System.out.println("Height:-"+height+" Width:-"+width);
}
}
class Chair extends Furniture
{
int lenght=30;
public void disp_chair()
{
System.out.println("Length:-"+lenght);
}
}
6. public class FurnitureTest {
public static void main(String args[])
{
Furniture f=new Furniture();
Chair c=new Chair();
System.out.println("Contents of superclass");
f.disp_furniture();
System.out.println("Contents of derived class");
c.disp_furniture();
c.disp_chair();
}
}
• OUTPUT:-
Contents of superclass
Height:-10 Width:-20
Contents of derived class
Height:-10 Width:-20
Length:-30
8. • Example:-
class Marks
{
int sub1=30, sub2=40,sub3=45;
void disp_marks()
{
System.out.println(sub1+" "+sub2+" "+sub3);
}
}
class Average extends Marks
{
int total;
float avg;
void disp_avg()
{
total=sub1+sub2+sub3;
avg=total/3;
System.out.println("Average:- "+avg);
}
}
9. class Percentage extends Average
{
double per;
void disp_per()
{
per=total/150;
per=per*100;
System.out.println("Percentage:- "+per);
}
}
public class Furniture
{
public static void main(String args[])
{
Percentage p=new Percentage();
p.disp_marks();
p.disp_avg();
p.disp_per();
}
}
OUTPUT:-
30 40 45
Average:- 38.0
Percentage:- 76.66
10. HIERARCHICAL INHERITENCE
• Many classes are derived from a single superclass.
• One class can be parent of many other classes.
11. WHY MULTIPLE INHERITENCE IS NOT
SUPPORTED IN JAVA?
• In multiple inheritance properties and behaviour of
multiple classes are inherited into a single class.
• Simplicity can be a reason for not including multiple
inheritance in Java
• Or logically Diamond problem can be a reason