Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oops java #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions abstract1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class abstract1{
public static void main(String args[]){
// Horse h=new Horse();
// h.eat();
// h.walk();
// System.out.println(h.color);
// Chicken c=new Chicken();
// c.eat();
// c.walk();
Mustang myHorse=new Mustang();
// Animal4 a =new Animal4(); can't create object of abstract class
}

}
abstract class Animal4{
Animal4(){
System.out.println("animal constructor called");
}
String color;
// Animal4(){
// color="brown";
// }
void eat(){
System.out.println("animal eats");
}
abstract void walk();
}
class Horse extends Animal4{
Horse(){
System.out.println("horse constructor called");
}
void changeColor(){
color="dark brown";
}
void walk(){
System.out.println("walks on 4 legs");
}
}
class Mustang extends Horse{
Mustang(){
System.out.println("Mustang constructor called");
}
}
class Chicken extends Animal4{
void walk(){
System.out.println("walks on 2 legs");
}
}
13 changes: 13 additions & 0 deletions encapsulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class encapsulation{
public static void main(String args[]){
//System.out.println("encapsulation");
Student1 s1=new Student1();
}
}
class Student1{
String name;
int roll;
Student1(){
System.out.println("constructor is called...");
}
}
36 changes: 36 additions & 0 deletions hierarichal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class hierarichal {
public static void main(String args[]){
Bird dobby1=new Bird();
dobby1.eat();
// dobby1.legs=4;
// System.out.println(dobby1.legs);
dobby1.breathe();
dobby1.fly();

}
}
//base class
class Animal2{
String color;
void eat(){
System.out.println("eats");
}
void breathe(){
System.out.println("breathes");
}
}
//derived class / subclass
class Mammal1 extends Animal2 {
void walk(){
System.out.println("walks");
}
}class Fish1 extends Animal2 {
void swim(){
System.out.println("swim");
}
}class Bird extends Animal2 {
void fly(){
System.out.println("fly");
}
}

23 changes: 23 additions & 0 deletions inheritance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class inheritance {
public static void main(String args[]){
Fish shark=new Fish();
shark.eat();
}
}
//base class
class Animal{
String color;
void eat(){
System.out.println("eats");
}
void breathe(){
System.out.println("breathes");
}
}
//derived class / subclass
class Fish extends Animal{
int fins;
void swim(){
System.out.println("swims in water");
}
}
34 changes: 34 additions & 0 deletions interfaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class interfaces{
public static void main(String args[]){
Queen q=new Queen();
q.moves();
}
}
interface ChessPlayer{
void moves();
}
interface Herbivore{

}
interface Carnivore{

}
class Bear implements Herbivore,Carnivore{

}
class Queen implements ChessPlayer{

public void moves() {
System.out.println("up down left right diagonal (in all 4dirns)");
}
}
class Rook implements ChessPlayer{
public void moves(){
System.out.println("up down left right");
}
}
class King implements ChessPlayer{
public void moves(){
System.out.println("up down left right diagonal-(by 1 step)");
}
}
55 changes: 55 additions & 0 deletions lec1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class lec1 {
public static void main(String args[]) {
// Pen p1 = new Pen(); //created a pen object called p1
// p1.setColor("Blue");
// System.out.println(p1.color);
// p1.setTip(5);
// System.out.println(p1.tip);
// p1.color="Yellow";
// System.out.println(p1.color);
// BankAccount myAcc=new BankAccount();
// myAcc.username="shraddhaKhapra";
// myAcc.setPassword("abcdefghi");
Pen p1 = new Pen(); //created a pen object called p1
p1.setColor(("Blue"));
System.out.println(p1.getColor());
p1.setTip(5);
System.out.println(p1.getTip());
p1.setColor("Yellow");
System.out.println(p1.getColor());
}
}
//class BankAccount{
// public String username;
// private String password;
//
// public void setPassword(String pwd) {
// password = pwd;
// }
//}
class Pen{
private String color;
private int tip;
String getColor(){
return this.color;
}
int getTip(){
return this.tip;
}

void setColor(String newColor){
color=newColor;
}
void setTip(int newTip){
tip=newTip;
}
}
class Student{
String name;
int age;
float percentage;//cgpa

void calcPercentage(int phy,int chem,int math){
percentage=(phy+chem+math)/3;
}
}
19 changes: 19 additions & 0 deletions method_overloading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class method_overloading{
public static void main(String args[]){
Calculator calc=new Calculator();
System.out.println(calc.sum(1,2));
System.out.println(calc.sum((float)1.5,(float)2.5));
System.out.println(calc.sum(1,2,3));
}
}
class Calculator{
int sum(int a,int b){
return a+b;
}
float sum(float a,float b){
return a+b;
}
int sum(int a ,int b ,int c){
return a+b+c;
}
}
17 changes: 17 additions & 0 deletions method_overriding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class method_overriding{
public static void main(String args[]){
Deer d=new Deer();
d.eat();
}

}
class Animal3 {
void eat(){
System.out.println("eats anything");
}
}
class Deer extends Animal{
void eat(){
System.out.println("eats grass");
}
}
27 changes: 27 additions & 0 deletions multiLevel_Inheritance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class multiLevel_Inheritance {
public static void main(String args[]){
Dog dobby=new Dog();
dobby.eat();
dobby.legs=4;
System.out.println(dobby.legs);

}
}
//base class
class Animal1{
String color;
void eat(){
System.out.println("eats");
}
void breathe(){
System.out.println("breathes");
}
}
//derived class / subclass
class Mammal extends Animal1 {
int legs;
}
class Dog extends Mammal{
String breed;
}

9 changes: 9 additions & 0 deletions packages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package myPackage;
import java.util.*;
public class packages{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println(a);
}
}
32 changes: 32 additions & 0 deletions static_keyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class static_keyword {

public static void main(String args[]) {
Student3 s1=new Student3();
s1.schoolName="MVN";

Student3 s2=new Student3();
System.out.println(s2.schoolName);

Student3 s3=new Student3();
s3.schoolName="ABC";
System.out.println(s2.schoolName);



}
}
class Student3{
static int returnPercentage(int math,int phy,int chem){
return (math+phy+chem)/3;
}
String name;
int roll;

static String schoolName;
void setName(String name){
this.name=name;
}
String getName(){
return this.name;
}
}
19 changes: 19 additions & 0 deletions super_keyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class super_keyword{
public static void main(String args[]){
Horse1 h=new Horse1();
System.out.println(h.color);
}

}
class Animal5{
String color;
Animal5(){
System.out.println("animal constructor is called");
}
}
class Horse1 extends Animal5{
Horse1(){
super.color="brown";
System.out.println("horse constructor is called");
}
}
Loading