Você está na página 1de 23

Java

Inheritance

Inheritance is a mechanism in which one object acquires all the


properties and behaviours of parent object.
The idea behind inheritance is that you can create new classes that are
built upon existing classes.
When you inherit from an existing class, you reuse (or inherit) methods
and fields, and you add new methods and fields to adapt your new class
to new situations.
Inheritance represents the IS-A relationship.
Why use Inheritance?
For Method Overriding (So Runtime Polymorphism).
For Code Reusability.
class Subclass-name extends Superclass-name
{
//methods and fields
}
The keyword extends indicates that you are making a new class that
derives from an existing class.
In the terminology of Java, a class that is inherited is called a
superclass. The new class is called a subclass.

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;

1. BedRoom( int x, int y, int z)


2. {
3. super(x,y);
4. height = z;
5. }
6. int vol()
7. {
8. return(length * breadth * height);
9. }
10.}
11.
class InherTest
12.{
13.public static void main(String args[])
14.{
15.BedRoom room1= new
BedRoom(2,3,4);
16.int area1= room1.area();
17.int vol1= room1.vol();
18.System.out.println(area1);
19. System.out.println(vol1);
20. }
21. }

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.

Default constructor of superclass


is available to subclass by default
class One {
One()
{
System.out.println("one");
}
}
class Two extends One {
Two()
{
System.out.println("Two");
}
}
class Super1 {
public static void main(String args[]) {
Two subOb = new Two();
}
}

Output:
one
Two

Using super to Call Superclass variable


class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed); //will print speed of Vehicle now
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
Output:
50
}

Using super to Call Superclass constructor


class One {
One(int i)
{
System.out.println(i);
}
}
class Two extends One {
Two(int a)
{
super(a);
System.out.println("Two");
}
}
class Super1 {
public static void main(String args[]) {
Two subOb = new Two(20);
}
}

Output:
20
Two

Using super to Call Superclass method


class Person{
void message()
{System.out.println("welcome");}
}
class Student extends Person{
void message()
{System.out.println("welcome to java");}
void display(){
message(); //will invoke current class message() method
super.message(); //will invoke parent class message() method
}
public static void main(String args[]){
Output:
Student s=new Student();
welcome to java
s.display();
welcome
}
}

Using super to Call Superclass method


class Person{
void message()
{System.out.println("welcome");}
}
class Student extends Person{
void message()
{System.out.println("welcome to java");}
void display(){
message(); //will invoke current class message() method
super.message(); //will invoke parent class message() method
}
public static void main(String args[]){
Output:
Student s=new Student();
welcome to java
s.display();
welcome
}
}

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

4. class Rectangle implements


Area
5. {
6. public float compute(float x,
float y)
7. { return (x *y); } }

8. class Circle implements Area


9. { public float compute (float
x, float y)
10.{ return (pi * x *x); } }

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

class Results extends Test


implements Sports {float total;
public void putWt() {
System.out.println("Sports Wt = "
+sportWt);}
void display() {
total = part1 + part2 + sportWt;
putNumber();
putMarks();
putWt();
System.out.println("Total Score ="
+total); }}

class Hybrid extends Results {


public static void main(String[]
args) {
Results student1 = new Results();
student1.getNumber(1234);
student1.getMarks(27.5F, 33.0F);
student1.display();
}}

Student
Test

Sports
Result
Hybrid

Você também pode gostar