0% found this document useful (0 votes)
8 views12 pages

PDF2 OOP Java Programs

The document provides an overview of Object-Oriented Programming concepts in Java, including classes, constructors, encapsulation, inheritance, polymorphism, abstract classes, interfaces, static members, inner classes, enums, and method overriding. Each concept is illustrated with code examples demonstrating their usage. The document serves as a comprehensive guide for understanding the fundamentals of Java programming.

Uploaded by

ramanjicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

PDF2 OOP Java Programs

The document provides an overview of Object-Oriented Programming concepts in Java, including classes, constructors, encapsulation, inheritance, polymorphism, abstract classes, interfaces, static members, inner classes, enums, and method overriding. Each concept is illustrated with code examples demonstrating their usage. The document serves as a comprehensive guide for understanding the fundamentals of Java programming.

Uploaded by

ramanjicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Object-Oriented Java

1. Class and Object


Basic class and object creation.
class Person {
String name;
int age;
Person(String n, int a) { name = n; age = a; }
void info() { System.out.println(name+" - "+age); }
}
public class PersonDemo {
public static void main(String[] args) {
Person p = new Person("Raman", 30);
p.info();
}
}

Page 1 of 12
2. Constructor Overloading
Multiple constructors example.
class Box {
int w,h;
Box() { w=h=1; }
Box(int x,int y) { w=x; h=y; }
}
public class BoxDemo { public static void main(String[] args){ Box b1=new Box(); Box b2=ne
w Box(3,4); System.out.println(b2.w*b2.h); }}

Page 2 of 12
3. Encapsulation
Private fields with getters/setters.
class Encapsulate {
private int value;
public int getValue(){ return value; }
public void setValue(int v){ if (v>=0) value=v; }
}
public class EncapsulateDemo { public static void main(String[] args){ Encapsulate e=new E
ncapsulate(); e.setValue(10); System.out.println(e.getValue()); }}

Page 3 of 12
4. Inheritance
Subclass extends superclass.
class Animal { void sound(){ System.out.println("Some sound"); }}
class Dog extends Animal { void sound(){ System.out.println("Bark"); }}
public class InheritDemo{ public static void main(String[] args){ Animal a=new Dog(); a.so
und(); }}

Page 4 of 12
5. Polymorphism
Method overriding & dynamic dispatch.
class Shape { void draw(){ System.out.println("Shape"); }}
class Circle extends Shape { void draw(){ System.out.println("Circle"); }}
public class PolyDemo{ public static void main(String[] args){ Shape s=new Circle(); s.dra
w(); }}

Page 5 of 12
6. Abstract Class
Abstract methods and class.
abstract class Vehicle { abstract void move(); }
class Car extends Vehicle { void move(){ System.out.println("Car moves"); }}
public class AbstractDemo{ public static void main(String[] args){ Vehicle v=new Car(); v.
move(); }}

Page 6 of 12
7. Interface
Multiple inheritance via interfaces.
interface Flyable { void fly(); }
class Bird implements Flyable { public void fly(){ System.out.println("Bird flying"); }}
public class InterfaceDemo{ public static void main(String[] args){ Flyable f=new Bird();
f.fly(); }}

Page 7 of 12
8. Static Members
Static field and method example.
class Counter { static int count=0; Counter(){ count++; } }
public class StaticDemo{ public static void main(String[] args){ new Counter(); new Counte
r(); System.out.println(Counter.count); }}

Page 8 of 12
9. Inner Class
Non-static inner class usage.
class Outer { class Inner { void msg(){ System.out.println("Hello from inner"); } } }
public class InnerDemo{ public static void main(String[] args){ Outer o=new Outer(); Outer
.Inner i=o.new Inner(); i.msg(); }}

Page 9 of 12
10. Enum
Define and use enum.
enum Day { MON, TUE, WED }
public class EnumDemo{ public static void main(String[] args){ for (Day d: Day.values()) S
ystem.out.println(d); }}

Page 10 of 12
11. ToString Override
Override toString for readable output.
class Point { int x,y; Point(int x,int y){this.x=x;this.y=y;} public String toString(){ re
turn "("+x+","+y+")"; }}
public class ToStringDemo{ public static void main(String[] args){ System.out.println(new
Point(2,3)); }}

Page 11 of 12
12. Equals and HashCode
Override equals() and hashCode().
class Item { int id; Item(int id){this.id=id;} public boolean equals(Object o){ if(!(o ins
tanceof Item)) return false; return id==((Item)o).id;} public int hashCode(){ return Integ
er.hashCode(id);} }
public class EqualsDemo{ public static void main(String[] args){ System.out.println(new It
em(1).equals(new Item(1))); }}

Page 12 of 12

You might also like