Escolar Documentos
Profissional Documentos
Cultura Documentos
Inheritance
Inheritance Basics
This is how the extends keyword is used to
achieve inheritance.
public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}
Is a example
class Employee
{
float salary=40000; //can be protected or public
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output
Types of Inheritance
Types of Inheritance
Single Inheritance
1. class Animal{
2.
public void move(){
3.
System.out.println("Animals can move");
4.
}
5. }
6. class Dog extends Animal{
7.
public void move(){
8.
System.out.println("Dogs can walk and run");}}
9. class TestDog{
10.
public static void main(String args[]){
11.
//Animal a = new Animal();
12.
Animal reference and object
13.
Dog b = new Dog();
14.
15.
a.move();// runs the method in Animal class
16.
b.move();//Runs the method in Dog class
17.
}
18.}
class Calculation
{
int z;
public void addition(int x, int y)
{ z=x+y;
System.out.println("The sum of the given numbers:"+z); }
public void Substraction(int x,int y)
{ z=x-y;
System.out.println("The difference between the given numbers:"+z);
}
public class My_Calculation extends Calculation
{ public void multiplication(int x, Programmer
int y)
{ z=x*y;
System.out.println("The product of the given numbers:"+z); }
public static void main(String args[])
{ int a=20, b=10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Substraction(a, b);
demo.multiplication(a, b); } }
Super
1. class Room
2. {
3. int length;
4. int breadth;
5. Room(int x, int y)
6. {
7. length= x;
8. breadth=y;
9. }
10.int area()
11.{
12.return (length * breadth);
13.}}
14. class BedRoom extends
Room
15. {
16. int height;
Super
Super may only be used in a subclass
constructor.
Call to superclass constructor must
be the first statement in subclass
constructor
Parameter of super call must match
the order or type of the instance
variable of the super class.
Output:
one
Two
Output:
20
Two
Multilevel Inheritance
1.
class A {
2.int x; int y;
3.int get(int p, int q){
4.x=p; y=q; return(0);
5.}
6.void Show(){
7.System.out.println(x);
8.}
9.}
10.
class B extends A{
11.
void Showb(){
12.
System.out.println("B");
13.
} }}
1.
class C extends B{
2.void display(){
3.System.out.println("C");
4.}
5.public static void
main(String args[]){
6.C c = new C();
7.c.get(5,6);
8.c.Show();
9.}
Interfaces
Interfaces are to support the concept
of multiple inheritance.
Java cannot be a subclass of more
than one superclass but it can
implements more than one interface.
Interface Item
{
Static final int code = 300;
String name=Fan;
void show();
}
interface ItemConstant
{
int code = 300;
String name= Fan;
}
interface ItemMethods
{
void display();
}
Interface Item extends ItemConstant, Itemmethods
Interface
1. interface Area
2. { final static float pi=3.14F;
3. float compute(float x, float y); }
1. class InterfaceTest
2. { public static void main
(String args[ ])
3. { Rectangle rect = new
Rectangle();
4. Circle cir = new Circle();
5. Area area;
6. area=rect;
7. System.out.println("Area" +
area.compute(10,20));
8. area=cir;
9. System.out.println(area.comp
ute(10,20));
10.}
11.}
class Student {
int rollNumber;
void getNumber(int n) {
rollNumber = n;}
void putNumber()
{System.out.println("Roll
NUmber: " + rollNumber); }}
class Test extends Student
{
float part1, part2;
void getMarks(float m1, float m2)
{
part1 = m1;
part2 = m2;
}
void putMarks() {
System.out.println("Marks
Obtained");
System.out.println("Part 1 =
"+part1);
System.out.println("Part 2 =
"+part2); }}
Student
Test
Sports
Result
Hybrid