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

Object-Oriented Java Concepts Explained

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)
13 views12 pages

Object-Oriented Java Concepts Explained

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

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() { [Link](name+" - "+age); }
}
public class PersonDemo {
public static void main(String[] args) {
Person p = new Person("Raman", 30);
[Link]();
}
}

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); [Link](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(); [Link](10); [Link]([Link]()); }}

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

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

Page 5 of 12
6. Abstract Class
Abstract methods and class.
abstract class Vehicle { abstract void move(); }
class Car extends Vehicle { void move(){ [Link]("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(){ [Link]("Bird flying"); }}
public class InterfaceDemo{ public static void main(String[] args){ Flyable f=new Bird();
[Link](); }}

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(); [Link]([Link]); }}

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

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: [Link]()) S
[Link](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){ [Link](new
Point(2,3)); }}

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

Page 12 of 12

Common questions

Powered by AI

Inner classes in Java serve the purpose of logically grouping classes that are only used in one place, increasing encapsulation, and can also access private members of the outer class. An example is class `Outer`, which contains a non-static inner class `Inner`. The message `"Hello from inner"` is printed when `msg()` is called on an instance of Inner, showing how inner classes operate within their outer class context .

Constructor overloading in Java allows a class to have more than one constructor with different parameter lists, enabling objects to be instantiated in different ways. An example is the Box class which has two constructors. The default constructor initializes the dimensions to 1: `Box() { w=h=1; }`, and the parameterized constructor allows for specific dimensions: `Box(int x,int y) { w=x; h=y; }`. This allows for creating boxes of default size or specific sizes .

Enums in Java represent a fixed set of constants. They offer type safety and can include methods and fields. Typically, enums are used to define a collection of constants. For example, the `Day` enum declares the days of the week as constants, which can then be iterated and used throughout the application to manage day-related logic effectively .

Java supports inheritance through the `extends` keyword, allowing a class to inherit properties and behaviors (methods) from another class. Method overriding occurs when a subclass provides a specific implementation of a method already present in its superclass. For instance, in Animal and Dog classes, the Dog class overrides the `sound()` method to output "Bark" instead of "Some sound", demonstrating inheritance and polymorphism .

An abstract class in Java provides a base for other classes to extend and can contain fields, constructors, and implemented methods. Abstract methods declared in an abstract class must be implemented by subclasses. In contrast, interfaces can only contain method signatures (until Java 8) and are aimed at providing a contract for classes that implement them, allowing multiple inheritance of type. An example is the Vehicle class, which is abstract and contains the abstract method `move()`, which is then implemented in the Car class .

Polymorphism in Java is demonstrated through method overriding and dynamic dispatch where a subclass provides a specific implementation of a method that is already defined in its superclass. This occurs at runtime, allowing for dynamic method resolution. For example, in the code `Shape s=new Circle(); s.draw();`, the draw method in the Circle class overrides the one in Shape class. Even though the reference type is Shape, the draw method of Circle is executed, illustrating dynamic dispatch .

A static member in Java is shared among all objects of a class, meaning that it is not tied to a particular object instance. This can be used to maintain a counter across all instances. For example, in the Counter class, the static field `count` is incremented each time a Counter object is created. When executed, `StaticDemo` prints `2` after creating two `Counter` objects, as `count` reflects the number of instances created .

Overriding the `toString()` method in Java enhances the program's readability and debugging processes by providing a human-readable depiction of an object. For example, in the class `Point`, `toString()` is overridden to output coordinates in the format `"(x,y)"`. When `System.out.println(new Point(2,3));` is run, it prints `(2,3)`, offering insight into the object's data rather than its memory address .

Encapsulation in object-oriented programming restricts direct access to some of an object's components and can prevent the unintended modification of data. In Java, encapsulation is implemented using private fields and public getter and setter methods. For instance, in the Encapsulate class, the `value` field is private, and the public methods `setValue()` and `getValue()` encapsulate access to this field, controlling how it is modified and accessed .

The `equals()` method in Java is overridden to provide a specific definition of equality for objects. This is crucial for comparing two objects meaningfully rather than just their references. For instance, in the Item class, `equals()` is overridden such that it checks if the ids of two Item objects are the same, which helps in comparing the logical data equality. This override allows customization according to class specifications and is essential for collections that rely on equality checks .

You might also like