oops.
Inheritance---
---------------------------
class person{
public int age;
public String name;
public String address;
}
class Student extends person{
public int marks;
public String grade;
Student(int age , String name , String address , int marks , String grade){
this.age=age;
this.name=name;
this.address=address;
this.marks=marks;
this.grade=grade;
}
public void disp(){
System.out.println("age is ="+age);
System.out.println("name is ="+name);
System.out.println("address is ="+address);
System.out.println("marks is ="+marks);
System.out.println("grade is ="+grade);
}
}
public class Main
{
public static void main(String[] args) {
Student s = new Student(22,"ved","bahjoi",98,"A");
s.disp();
}
}
------------------------------------------------------------------------------------------------------
class Plane{
public void takeOff(){
System.out.println("plane takeOff");
}
public void fly(){
System.out.println("plane fly");
}
public void land(){
System.out.println("plane land");
}
}
class CargoPlane extends Plane{
public void takeOff(){
System.out.println("CargoPlane takeOff");
}
public void fly(){
System.out.println("CargoPlane fly");
}
public void land(){
System.out.println("CargoPlane land");
}
}
class PassengerPlane extends Plane{
public void takeOff(){
System.out.println("PassengerPlane takeOff");
}
public void fly(){
System.out.println("PassengerPlane fly");
}
public void land(){
System.out.println("PassengerPlane land");
}
}
class FighterPlane extends Plane{
public void takeOff(){
System.out.println("FighterPlane takeOff");
}
public void fly(){
System.out.println("FighterPlane fly");
}
public void land(){
System.out.println("FighterPlane land");
}
}
public class Main
{
public static void main(String[] args) {
Plane p= new PassengerPlane();
p.takeOff();
p.fly();
p.land();
System.out.println();
Plane c= new CargoPlane();
c.takeOff();
c.fly();
c.land();
System.out.println();
Plane f= new FighterPlane();
f.takeOff();
f.fly();
f.land();
System.out.println();
}
}
----------------------------------------------------------------------------------------------------------------
class Plane{
public void takeOff(){
System.out.println("plane takeOff");
}
public void fly(){
System.out.println("plane fly");
}
public void land(){
System.out.println("plane land");
}
}
class CargoPlane extends Plane{
public void takeOff(){
System.out.println("CargoPlane takeOff");
}
public void fly(){
System.out.println("CargoPlane fly");
}
public void land(){
System.out.println("CargoPlane land");
}
}
class PassengerPlane extends Plane{
public void takeOff(){
System.out.println("PassengerPlane takeOff");
}
public void fly(){
System.out.println("PassengerPlane fly");
}
public void land(){
System.out.println("PassengerPlane land");
}
}
class FighterPlane extends Plane{
public void takeOff(){
System.out.println("FighterPlane takeOff");
}
public void fly(){
System.out.println("FighterPlane fly");
}
public void land(){
System.out.println("FighterPlane land");
}
}
class Airport{
public void aeroplane(Plane p){
p.takeOff();
p.fly();
p.land();
System.out.println();
}
}
public class Main
{
public static void main(String[] args) {
Plane p= new PassengerPlane();
Plane c= new CargoPlane();
Plane f= new FighterPlane();
Airport a = new Airport();
a.aeroplane(p);
a.aeroplane(c);
a.aeroplane(f);
}
}
-----------------------------------------------------------------------------------------------------------------\
abstraction--
----------------------
abstract class Plane {
public abstract void takeOff();
public abstract void fly();
public abstract void land();
}
class CargoPlane extends Plane {
public void takeOff() {
System.out.println("CargoPlane takeOff");
}
public void fly() {
System.out.println("CargoPlane fly");
}
public void land() {
System.out.println("CargoPlane land");
}
}
class FighterPlane extends Plane {
public void takeOff() {
System.out.println("FighterPlane takeOff");
}
public void fly() {
System.out.println("FighterPlane fly");
}
public void land() {
System.out.println("FighterPlane land");
}
}
class Passengerlane extends Plane {
public void takeOff() {
System.out.println("Passengerlane takeOff");
}
public void fly() {
System.out.println("Passengerlane fly");
}
public void land() {
System.out.println("Passengerlane land");
}
}
class Airport{
public void allowPlane(Plane ref){
ref.takeOff();
ref.fly();
ref.land();
System.out.println();
}
}
public class Main
{
public static void main(String[] args) {
Airport a = new Airport();
a.allowPlane(new CargoPlane());
a.allowPlane(new Passengerlane());
a.allowPlane(new FighterPlane());
}
}
-------------------------------------------------------------------------------------------------------------------------
constructor in abstract class ---
abstract class Person {
String name;
int age;
int height;
Person(String name,int age , int height){
this.name=name;
this.age=age;
this.height=height;
}
}
class Student extends Person{
int marks;
float Average;
Student(String name , int age , int height , int marks , float Average){
super(name,age,height);
this.marks=marks;
this.Average=Average;
}
public void disp(){
System.out.println("name is="+name);
System.out.println("age is="+age);
System.out.println("height is="+height);
System.out.println("marks is="+marks);
System.out.println("Average is="+Average);
}
}
public class Main
{
public static void main(String[] args) {
Student s = new Student("ved",22,5,100,5.7f);
s.disp();
}
}
-------------------------------------------------------------------------------------------------------------------------
abstract class Bird{
public abstract void eat();
public abstract void fly();
}
class crow extends Bird{
public void eat(){
System.out.println("crow eats grains");
}
public void fly(){
System.out.println("crow fly at medium level");
}
}
class sparrow extends Bird{
public void eat(){
System.out.println("sparrow eats grains");
}
public void fly(){
System.out.println("sparrow fly at short level");
}
}
abstract class Eagle extends Bird{
public abstract void eat();
public void fly(){
System.out.println("Eagle fly at very high level");
}
}
class SerpentEagle extends Eagle{
public void eat(){
System.out.println("Serpent Eagle eats snake");
}
}
class GoldenEagle extends Eagle{
public void eat(){
System.out.println("Golden Eagle catch prey over the ocean");
}
}
class Sky{
public void allowBird(Bird ref){
ref.eat();
ref.fly();
System.out.println();
}
}
public class Main
{
public static void main(String[] args) {
Sky s = new Sky();
s.allowBird(new crow());
s.allowBird(new sparrow());
s.allowBird(new SerpentEagle());
s.allowBird(new GoldenEagle());
}
}
--------------------------------------------------------------------------------------------------------
Comments
Post a Comment